lundi 27 février 2017

found error in laravel 5.4,Property [categories] does not exist on this collection instance

I am trying to display the product listing on listing page of the product. Each product has category.My table structure

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 category

Category Model
         public function products() {
        return $this->hasMany("App\Product");
    }

This is the Category model where the Category has many product

Now 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 be

products
        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."



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2myt1DB
via IFTTT

Aucun commentaire:

Enregistrer un commentaire