Laravel 5.2雄辩查询

时间:2016-06-24 10:08:58

标签: eloquent laravel-5.2

我有2个表JobsDepartments(一个工作属于某个部门),我想在其部门找到一些工作。
这是我必须修改的代码:

    $query = Job::query()->where('status', 2);

    if($input['title']!=""){
        $query->where('title', 'like', '%' . $input['title'] . '%')->get();
    }
    if($input['location'] != ""){
        $query->where('location', '=', $input['location']);
    }
    if($input['recruiters'] != ""){
        $query->where('recruiter_id','=', $input['recruiters']);
    }

    $jobs = $query->paginate(5);
    return $jobs;

query变量对我没有方法join。我试试with但它不起作用。 join有任何解决方案,或通过DB进行其他查询,但并不总是设置title,location,recruiters个变量。
抱歉我的英语不好,谢谢你的考虑。

1 个答案:

答案 0 :(得分:0)

看看雄辩的关系:https://laravel.com/docs/5.1/eloquent-relationships#defining-relationships

您基本上在模型中定义Job和Depertment之间的关系,然后调用$job->department()->name(这是一个示例调用)来获取所附部门的名称。