Hassan Ali
Hassan Ali

Reputation: 35

Not able to fetch data from primary table in laravel

I have 3 tables i.e category, sub category and products. I have passed category id to sub category and then sub category id to product so that i can get the whole data as per requirements. The problem in i an able to get the data from the secondary tables that has joins statement not from the primary table.Here is my code of query. Any help from you would be appreciated

public function showProduct()
    {
  
        $data = DB::table('category')
        ->join('sub_category','category.id',"=",'sub_category.category_id')
        ->join('product','sub_category.id', '=', 'product.sub_category_id')
        ->get();

        return $this->success('date' ,$data);

    }

Upvotes: 0

Views: 41

Answers (1)

John Lobo
John Lobo

Reputation: 15319

use leftjoin

 $data = DB::table('category')
    ->leftjoin('sub_category','category.id',"=",'sub_category.category_id')
    ->leftjoin('product','sub_category.id', '=', 'product.sub_category_id')
    ->get();

Upvotes: 2

Related Questions