mardi 2 juillet 2019

i want to get all products and category name of each product from category table - Laravel

i want to get all products and get each product categorey name from category table .. anyone can help ???

  public function viewProducts(Request $request){

 $products = Product::get();

 foreach($products as $key => $val){
  $category_name = Category::where(['id' => $val- >category_id])->first();
  $products[$key]->category_name = $category_name->name;
 }
 $products = json_decode(json_encode($products));
 //echo "<pre>"; print_r($products); die;
   return view('admin.products.view_products')->with(compact('products'));
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire