Laravel Eloquent多条件内连接

yi0zb3m4  于 2022-12-24  发布在  其他
关注(0)|答案(7)|浏览(133)

我有一个关于多个值的内部连接的问题。我确实在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"));

    });
}

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

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 (                                                                                        ))

正如您所看到的,join中的多个条件都很好,但是它认为1是一个列而不是字符串,这是可能的吗?还是必须在where中修复它?

y53ybaqx

y53ybaqx1#

//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);
crcmnpdw

crcmnpdw2#

因为你这样做的方式,它认为这两个都是连接条件在您的代码如下:

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");
    });
}

因此,您应该删除第二行:

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

现在,您应该添加一个where子句,它应该如下所示:

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

fkaflof63#

您只需将多个条件作为where()添加到连接闭包中即可

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

7cwmlq894#

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;

希望能有所帮助;)

6ioyuze2

6ioyuze25#

这在政治上并不正确,但确实有效

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

bis0qfac6#

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();
fruv7luv

fruv7luv7#

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

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

或者别的解决方法

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

相关问题