php 具有多个条件的 Laravel Eloquent 内连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20731606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:28:50  来源:igfitidea点击:

Laravel Eloquent inner join with multiple conditions

phplaraveleloquent

提问by Wouter Neuteboom

I have a question about inner joins with multiple on values. I did build my code like this in laravel.

我有一个关于具有多个值的内部连接的问题。我确实在 Laravel 中构建了这样的代码。

public function scopeShops($query) {
    return $query->join('kg_shops', function($join)
    {
        $join->on('kg_shops.id', '=', 'kg_feeds.shop_id');
        // $join->on('kg_shops.active', '=', "1"); // WRONG
        // EDITED ON 28-04-2014
        $join->on('kg_shops.active', '=', DB::raw("1"));

    });
}

Only problem is, it gives this outcome:

唯一的问题是,它给出了这个结果:

Column not found: 1054 Unknown column '1' in 'on clause' (SQL: select `kg_feeds`.* from `kg_feeds` inner join `kg_shops` on `kg_shops`.`id` = `kg_  
  feeds`.`shop_id` and `kg_shops`.`active` = `1`) (Bindings: array (                                                                                        )) 

As you can see, the multiple conditions in the join go fine, but it thinks the 1is a column instead of a string. Is this even possible, or do I have to fix it in the where.

如您所见,连接中的多个条件都很好,但它认为1是列而不是字符串。这甚至可能吗,还是我必须在那里修复它。

Thanks in advance!

提前致谢!

回答by rama

return $query->join('kg_shops', function($join)
 {
   $join->on('kg_shops.id', '=', 'kg_feeds.shop_id');

 })
 ->select('required column names') 
 ->where('kg_shops.active', 1)
 ->get();

回答by Majbah Habib

You can see the following code to solved the problem

可以看到下面的代码来解决问题

return $query->join('kg_shops', function($join)
{
    $join->on('kg_shops.id', '=', 'kg_feeds.shop_id');
    $join->where('kg_shops.active','=', 1);
});

Or another way to solved it

或者另一种方法来解决它

 return $query->join('kg_shops', function($join)
{
    $join->on('kg_shops.id', '=', 'kg_feeds.shop_id');
    $join->on('kg_shops.active','=', DB::raw('1'));
});

回答by The Alpha

Because you did it in such a way that it thinks both are join conditions in your code given below:

因为您这样做的方式是它认为两者都是下面给出的代码中的连接条件:

public function scopeShops($query) {
    return $query->join('kg_shops', function($join)
    {
        $join->on('kg_shops.id', '=', 'kg_feeds.shop_id');
        $join->on('kg_shops.active', '=', "1");
    });
}

So,you should remove the second line:

所以,你应该删除第二行:

return $query->join('kg_shops', function($join)
{
    $join->on('kg_shops.id', '=', 'kg_feeds.shop_id');
});

Now, you should add a where clause and it should be like this:

现在,您应该添加一个 where 子句,它应该是这样的:

return $query->join('kg_shops', function($join)
{
  $join->on('kg_shops.id', '=', 'kg_feeds.shop_id')->where('kg_shops.active', 1);
})->get();

回答by Yagnesh bhalala

//You may use this example. Might be help you...

$user = User::select("users.*","items.id as itemId","jobs.id as jobId")
        ->join("items","items.user_id","=","users.id")
        ->join("jobs",function($join){
            $join->on("jobs.user_id","=","users.id")
                ->on("jobs.item_id","=","items.id");
        })
        ->get();
print_r($user);

回答by ThangTD

More with where in (list_of_items):

更多与where in (list_of_items)

    $linkIds = $user->links()->pluck('id')->toArray();

    $tags = Tag::query()
        ->join('link_tag', function (JoinClause $join) use ($linkIds) {
            $joinClause = $join->on('tags.id', '=', 'link_tag.tag_id');
            $joinClause->on('link_tag.link_id', 'in', $linkIds ?: [-1], 'and', true);
        })
        ->groupBy('link_tag.tag_id')
        ->get();

    return $tags;

Hope it helpful ;)

希望它有帮助;)

回答by Kamlesh

You can simply add multiple conditions by adding them as where() inside the join closure

您可以通过在 join 闭包中将它们添加为 where() 来简单地添加多个条件

->leftJoin('table2 AS b', function($join){
        $join->on('a.field1', '=', 'b.field2')
        ->where('b.field3', '=', true)
        ->where('b.field4', '=', '1');
})

回答by Uncoke

This is not politically correct but works

这在上不正确,但有效

   ->leftJoin("players as p","n.item_id", "=", DB::raw("p.id_player and n.type='player'"))