Query and Sub Query Join laravel

I want to join subcategory first and subscribe and it must be a subquery to 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();

      

I need to look like this

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

      

I tried to execute query and model but I get a view like this

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

      

+4


source to share


2 answers


You can solve this problem according to your opinion. Using @foreach should be helpful. However, using Eloquent and Models is the "Laravel way". (This should be less of a hassle in the long run)



0


source


Try this, I used a function joinSub

instead join

it might work according to your needs.



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

      

0


source







All Articles