RelationNotFoundException.php中的RelationNotFoundException

时间:2016-10-05 15:45:28

标签: laravel laravel-5

每个产品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');
    }
}

1 个答案:

答案 0 :(得分:2)

查看代码时,不能将:: class与with()函数一起使用。主要原因是:: class将返回带有命名空间的完整路径。

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

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