Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 具有多个条件的Laravel雄辩内连接_Php_Laravel_Eloquent - Fatal编程技术网

Php 具有多个条件的Laravel雄辩内连接

Php 具有多个条件的Laravel雄辩内连接,php,laravel,eloquent,Php,Laravel,Eloquent,我有一个关于值上有多个内部联接的问题。 我确实在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', '=', "

我有一个关于值上有多个内部联接的问题。 我确实在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 (                                                                                        )) 
正如您所看到的,联接中的多个条件很好,但它认为
1
是一个列而不是一个字符串。这是可能的,还是我必须在哪里修复它


提前谢谢

因为您这样做,它认为这两个条件都是下面代码中的连接条件:

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();
有关
的更多信息,请参见(项目列表)


希望对您有所帮助;)

您可以看到以下代码来解决此问题

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

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

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

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

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

嗨,谢赫,谢谢你的回答,但我试过了。给我下面的结果。PHP致命错误:调用第35行/app/models/Feeds.PHP中的undefined方法illumb\Database\Query\JoinClause::where(),如何添加多个带有或条件的联接?喜欢加入这个还是加入那个?
$join->On()->>orOn()
。不用使用
数组映射
,你可以这样做:
$linkIds=$links->列表('id','id')
,如果你有使用
hasMany()
pivot()
的有说服力的模型,也可以。非常感谢你的建议,我已经很久没有找到它了:(有时事情可能没有文档记录,但我倾向于查看上的源代码或:D我也一样,我会尽量减少不必要的代码,实际上我浪费了时间去挖掘一个像你给出的函数,但放弃了:(它只是
pulk
的别名,一旦使用
get
,它就不起作用了;因此在您的例子中,
$LinkId=$user->links()->pulk('id'))
如果我打印查询日志,那么它将在两个条件之间显示
。但是我如何使用
而不是
使用它。$join->orOn('kg\u shops.active','=','DB::raw('1');如果'active'字段包含字符串,则必须使用DB::raw('YES'),而不是DB::raw('YES'))。您可以在连接中添加where子句,它会认为“1”不是一列,正如Kamlesh所说。
//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);
->leftJoin('table2 AS b', function($join){
        $join->on('a.field1', '=', 'b.field2')
        ->where('b.field3', '=', true)
        ->where('b.field4', '=', '1');
})
   ->leftJoin("players as p","n.item_id", "=", DB::raw("p.id_player and n.type='player'"))