我的滔滔不绝的模型存在一些问题,我尝试在一个
中加入两个不同的表现在我有两张表,比如。
第一个表名为“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 |
我和whana得到这样的表。
|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') );
它正在运行,但我只需要获取employees table中的行,其中company_id = 2(在我的示例中);
Session::get('company_id') = 2
我将通过在laravel.log文件中记录来检查它。
我认为这是问题所在('company_saved_cv.company_id','=',Session :: get('company_id'));请求,但我无法弄清楚,也许有人可以帮助我?
答案 0 :(得分:8)
我用这段代码解决了这个问题
$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."'"));
})
答案 1 :(得分:2)
当你使用LEFT JOIN作为INNER JOIN
的地方时使用LEFT JOIN和WHERE类似
SELECT workers.name_and_surname, workers.id, workers.location,company_saved_cv.worker_id
FROM workers LEFT JOIN company_saved_cv
ON company_saved_cv.worker_id= workers.id
WHERE company_saved_cv.company_id = 2
<强>输出强>
|id|location|name_and_surname|worker_id|
---------------------------------------|
|61|London | John John | 61 |
|66|London | John John | 66 |
使用LEFT JOIN和AND
SELECT workers.name_and_surname, workers.id, workers.location,company_saved_cv.worker_id
FROM workers LEFT JOIN company_saved_cv
ON company_saved_cv.worker_id= workers.id
AND company_saved_cv.company_id = 2
<强>输出强>
|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 |
新查询
$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')
->on('company_saved_cv.company_id', '=', Session::get('company_id') );
})
答案 2 :(得分:2)
或多或少是这样的:
$directoryTypeId = 1;
Node::query()
->leftJoin('directories', 'directories.id', '=', 'nodes.directory_id')
->leftJoin('directory_types', function ($leftJoin) use ($directoryTypeId) {
$leftJoin
->on('directory_types.id', '=', DB::raw($directoryTypeId))
->on('directory_types.COLUMN_1', '=', 'directories.COLUMN_2');
})
->select([
'nodes.id as node_id',
'directories.name',
'directory_types.id as type_id',
]);
答案 3 :(得分:0)
您可以轻松地使用模型。最后一个表,您可以删除该位置,因为您可以从模型中获取它。轻松创建多对多关系或您想要的东西。查看文档:{{3}}