gpt4 book ai didi

laravel - RelationNotFoundException.php 中的 RelationNotFoundException

转载 作者:行者123 更新时间:2023-12-02 21:02:58 24 4
gpt4 key购买 nike

每个产品hasMany属性。

当我使用 with 函数时:

dd(Product::with(ProductProperty::class)->get());

我收到此错误:

RelationNotFoundException in RelationNotFoundException.php 
Call to undefined relationship [App\Models\ProductProperty] on model [App\Models\Product].
<小时/>
class Product extends Model
{
protected $table = 'products';


protected $fillable = [
'user_id' ,'brand_id' , 'title', 'price', 'current_buy','max_buy','min_buy_per_bill',
'max_buy_per_bill','count','off','seri','short_description','long_description',
];



public function ProductProperty()
{
return $this->hasMany('App\Models\ProductProperty');
}
}
<小时/>
class ProductProperty extends Model
{
protected $table = 'products_properties';

protected $fillable = [
'product_id' ,'parent_id' , 'title','value', 'price', 'current_buy','max_buy','min_buy_per_bill',
'max_buy_per_bill','count','off','seri','short_description','long_description',
];

public function Product()
{
return $this->belongsTo('App\Models\Product');
}
}

最佳答案

查看您的代码,您无法将::class 与 with() 函数一起使用。主要原因是::class 将返回带有 namespace 的完整路径。

Product::with(ProductProperty::class)->get(); 不正确。

将其替换为 Product::with('ProductProperty')->get();

关于laravel - RelationNotFoundException.php 中的 RelationNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39878474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com