每个产品hasMany属性。
当我使用with函数时:
dd(Product::with(ProductProperty::class)->get());
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
RelationNotFoundException in RelationNotFoundException.php
Call to undefined relationship [App\Models\ProductProperty] on model [App\Models\Product].
Run Code Online (Sandbox Code Playgroud)
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');
}
}
Run Code Online (Sandbox Code Playgroud)
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');
}
}
Run Code Online (Sandbox Code Playgroud)
查看您的代码,您不能将 ::class 与 with() 函数一起使用。主要原因是 ::class 将返回带有命名空间的完整路径。
Product::with(ProductProperty::class)->get(); 是不正确的。
将其替换为 Product::with('ProductProperty')->get();
| 归档时间: |
|
| 查看次数: |
2106 次 |
| 最近记录: |