gpt4 book ai didi

php - Laravel LeftJoin where

转载 作者:可可西里 更新时间:2023-11-01 07:48:10 28 4
gpt4 key购买 nike

我的 eloquent 模型有一些问题,我尝试将两个不同的表合并为一个

现在我有两个表。

第一个表名称“company_saved_cv”

|id|company_id|worker_id |
--------------------------
|1| 2 | 61 |
|3| 3 | 66 |
|4| 2 | 69 |
--------------------------

第二个表名称为“workers”

|id|location|name_and_surname|
------------------------------
|61|London | John John |
|62|London | John John |
|66|London | John John |
|67|London | John John |
|68|London | John john |

我想得到这样的 table 。

|id|location|name_and_surname|worker_id|
---------------------------------------|
|61|London | John John | 61 |
|62|London | John John | NULL |
|66|London | John John | 66 |
|67|London | John John | NULL |
|68|London | John john | NULL |

我的模型函数在哪里

public static  function CvSearch($interested_field, $location)
{

$match = "MATCH( `interested_fields`) AGAINST (?)";

$query = Worker::select('workers.name_and_surname', 'workers.id', 'workers.location','company_saved_cv.worker_id')
->leftJoin('company_saved_cv', function($leftJoin)
{
$leftJoin->on('company_saved_cv.worker_id', '=', 'workers.id')
->where('company_saved_cv.company_id', '=', Session::get('company_id') );


})
->where('workers.location', '=', $location)
->whereRaw($match, array($interested_field))
->orderBy('workers.created_at')
->paginate(10);
return $query;
}

如果我评论这一行:->where('company_saved_cv.company_id', '=', Session::get('company_id') ); 它正在工作,但我只需要获取workers 表中 company_id = 2 的行(在我的示例中);

Session::get('company_id') = 2 

我会通过将其记录到 laravel.log 文件中来检查它。

我认为 where('company_saved_cv.company_id', '=', Session::get('company_id') ) 有问题;请求,但我无法弄清楚,也许有人可以帮助我?

最佳答案

我用这段代码解决了这个问题

 $query = Worker::select('workers.name_and_surname', 'workers.id', 'workers.location','company_saved_cv.worker_id')
->leftJoin('company_saved_cv', function($leftJoin)use($company_id)
{
$leftJoin->on('workers.id', '=', 'company_saved_cv.worker_id');
$leftJoin->on(DB::raw('company_saved_cv.company_id'), DB::raw('='),DB::raw("'".$company_id."'"));


})

关于php - Laravel LeftJoin where,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26711573/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com