在 laravel 5.4 中发现错误,此集合实例上不存在属性 [categories]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42499764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:25:39  来源:igfitidea点击:

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

phplaravellaravel-5eloquentlaravel-5.4

提问by vaibhav

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

这是类别表 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.

这是具有 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

这是Category 有很多产品的Category 模型

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

我发现了这个错误 "Property [categories] does not exist on this collection instance."

回答by Marcin Nabia?ek

When you run:

当你运行时:

$product = Product::with('categories')->get();

you are not getting single products but all products so it should be rather renamed to:

您得到的不是单一产品,而是所有产品,因此应该将其重命名为:

$products = Product::with('categories')->get();

Further, looking at your database structure, each product belongs to single category so in Productmodel you should rename

此外,查看您的数据库结构,每个产品都属于单个类别,因此在Product模型中您应该重命名

public function categories() 
{        
    return $this->belongsTo("App\Category");
}

to

public function category() 
{        
    return $this->belongsTo("App\Category");
}

If you do this change, you should then again change

如果您进行此更改,则应该再次更改

$products = Product::with('categories')->get();

to

 $products = Product::with('category')->get();

Going back to your controller assuming all your products have set category, you can display it like this:

回到你的控制器,假设你的所有产品都设置了类别,你可以这样显示:

foreach ($products as $product) 
{
   echo $product->id.' '.$product->name.' '.$product->description.' '.$product->category->name;
}

You can obviously do the same later in Blade view like so:

你显然可以稍后在 Blade 视图中执行相同的操作,如下所示:

@foreach ($list as $product) 
   {{ $product->id }} {{$product->name}} {{ $product->description }} {{ $product->category->name }}
@endforeach

回答by vaibhav

I have changed in ProductControllerwhich is my product controllers function

我改变了ProductController我的产品控制器功能

 public function index()
    {
        $products = Product::with('category')->get();        
        return view('product.index')->with("list",$products);
    }

Also changed in Product Model

产品型号也发生了变化

public function category() {
        return $this->belongsTo("App\Category");
    }

After these changes I got my expected result.

经过这些更改后,我得到了预期的结果。