查询和子查询加入laravel

时间:2017-04-19 06:34:44

标签: php jquery laravel laravel-5

我想首先加入子类别和subling,它应该是join mastercategory

的子查询
$users = DB::table('mastercategory')
->join('subcategory', 'mastercategory.idcategory', '=', 'subcategory.idcategory_mastercategory')
->join('subling', 'subcategory.idsubcategory', '=', 'subling.idsubcategory_subcategory')
->select('mastercategory.*', 'subcategory.*','subling.*')
->get();

我想要这样的视图

Mobile and Access.
    1. Mobile
      a. IPhone
      b. Nokia
    2. Mobile Cover

我尝试了上面的查询和模型,但我得到了这样的视图

Mobile and Access.
    1. Mobile
      a. IPhone
      b. Nokia
Mobile and Access.              
    2. Mobile Cover

1 个答案:

答案 0 :(得分:0)

尝试一下,我使用了joinSub函数而不是join,它可能会根据您的需要工作。

$subcategoryWithSubling = DB::table('subcategory')
->join('subling', 'subcategory.idsubcategory', '=', 
'subling.idsubcategory_subcategory')
->select('subcategory.*','subling.*');

$users = DB::table('mastercategory')
->joinSub($subcategoryWithSubling, 'subcategory', function($join) {
    $join->on('mastercategory.idcategory', '=', 
    'subcategory.idcategory_mastercategory')
})->get()