如何与别名模型建立联系

问题描述

您好,我有一个名为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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...