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'));
}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/30b8S9H
via IFTTT

Aucun commentaire:

Enregistrer un commentaire