categories
id name description
1 Cat1 Category 1
2 Cat2 Category 2
This is the category table having id name and description
products
id name description category_id
1 pro1 product 1 1
2 pro2 product 2 2
This is the product table having category_id.Product Model
public function categories() {
return $this->belongsTo("App\Category");
}
This is the product model where the products are belongs to categoryCategory Model
public function products() {
return $this->hasMany("App\Product");
}
This is the Category model where the Category has many productNow in the product controller on listing function I want the list of product with category name
public function index()
{
$product = Product::with('categories')->get();
print_r($product->categories);die;
return view('product.index')->with("list",$product);
}
I want my Output should beproducts
id name description category name
1 pro1 product 1 cat1
2 pro2 product 2 cat2
I found this error "Property [categories] does not exist on this collection instance."
via vaibhav