Php laravel中的查询和子查询联接

Php laravel中的查询和子查询联接,php,jquery,laravel,laravel-5,Php,Jquery,Laravel,Laravel 5,我想首先连接子类别和子类别,它应该是连接主类别的子查询 $users = DB::table('mastercategory') ->join('subcategory', 'mastercategory.idcategory', '=', 'subcategory.idcategory_mastercategory') ->join('subling', 'subcategory.idsubcategory', '=', 'subling.idsubcategory_subcate

我想首先连接子类别和子类别,它应该是连接主类别的子查询

$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
我尝试了上面的查询和模型,但我得到了这样的视图

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

试试看,我使用了
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()

您的选择是确定的,您需要编辑您的视图(您创建类别树的方式)。而且,如果您使用雄辩的关系,您是否尝试过
groupBy('mastercategory.title')