【发布时间】:2014-12-29 22:50:30
【问题描述】:
我的 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 |
我想得到这样的桌子。
|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') );请求,但我无法弄清楚,也许有人可以帮助我?
【问题讨论】:
-
实现结果的逻辑是什么。因为我看不到任何类型的关系 b/w 你的桌子。
-
闻起来像 XY 问题