Laravel LeftJoin在哪里 [英] Laravel LeftJoin where

查看:692
本文介绍了Laravel LeftJoin在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的雄辩模型存在一些问题,我尝试将两个不同的表合并为一个表

I have some problems with my eloquent model, where I try to join two different tables in one

现在我有两个类似的表.

Now I have two tables like.

第一个表的名称为"company_saved_cv"

First tables name "company_saved_cv"

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

第二张表命名为"workers"(工人)

Second tables name "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') );它正在工作,但是我只需要从worker表中获得company_id = 2的行(在我的示例中);

If I comment this line:->where('company_saved_cv.company_id', '=', Session::get('company_id') ); It is working, but I need get only rows from workers table where company_id = 2 (in my example);

Session::get('company_id') = 2 

我将通过将其记录在laravel.log文件中进行检查.

I will check it by logging it in laravel.log file.

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

I fink it is problem with where('company_saved_cv.company_id', '=', Session::get('company_id') ); request, but I can't figure it out, maybe some one can help me?

推荐答案

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

I solve this problem with this code

 $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."'"));


        })

这篇关于Laravel LeftJoin在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆