简体   繁体   English

Laravel雄辩的查询并在OR条件下加入

[英]Laravel eloquent query and join on OR condition

I am trying to do an eloquent query where it joins on a table where column a = x OR column b = x ; 我试图做一个雄辩的查询,它联接在表上,其中列a = x ORb = x and I cannot get it to work. 我无法使它工作。 So I am hoping that someone can help. 因此,我希望有人能提供帮助。

Here is my query: 这是我的查询:

$candidates = HrCandidate::where('people_id', '<>', 'NULL')

  ->with('contact')

  ->join(

    'people',

    ->where('id','people_id')

    ->orWhere('alternate_id','people_id')

  )

  ->get();

I am trying to join with the people table but where people_id = 1 or the alternate_id column. 我正在尝试加入人员表,但其中people_id = 1alternate_id列。 So I am hoping someone can help with this. 因此,我希望有人可以对此提供帮助。

To get started, pass a Closure as the second argument into the join method. 首先,将Closure作为第二个参数传递给join方法。 The Closure will receive a JoinClause object which allows you to specify constraints on the join clause: Closure将收到一个JoinClause对象,该对象使您可以指定join子句的约束:

$candidates = HrCandidate::join('people', function ($join) {
        $join
            ->on('people.id', '=', 'candidates.people_id')
            ->orOn('people.alternate_id', '=', 'candidates.people_id');
    })
    ->where('people_id', '<>', 'NULL')
    ->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM