微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何与别名模型建立联系

如何解决如何与别名模型建立联系

您好,我有一个名为Product的模型和两个名为ProductimageProductAttribute的模型

我希望什么? 我想访问产品属性并使用以下内容获取它们:

Product:with(['attribute','image','category']);

如果我将访问laravel属性,则返回此错误 Syntax error or access violation: 1066 Not unique table/alias: 'product_attributes'

在我的Product模型中

class Product extends Model
{
    protected $fillable = [
        'name','description','category_id',];

    protected $guarded = [
        'id',];

    protected $dates = [
        'created_at','updated_at',];

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function attribute()
    {
        return $this->belongsToMany(ProductAttributes::class,'product_attributes');
    }
}

在我的ProductAttribute模型中

class ProductAttributes extends Model
{
    protected $fillable = [
        'attribute','value','price','product_id','stock'
    ];

    public function product()
    {
        return $this->belongsTo(Product::class);
    }
}

评论:我正在使用laravel作为REST API

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。