【问题标题】:How to get polymorphic relationships in laravel?如何在laravel中获得多态关系?
【发布时间】:2020-10-13 03:11:12
【问题描述】:

我在 laravel 应用程序中使用了多态关系的 3 个模型和迁移

模型

  • 属性
  • 属性值
  • ModelHasAttribute

迁移

属性

Schema::create('attributes', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->timestamps();
});

属性值

Schema::create('attribute_values', function (Blueprint $table) {
    $table->id();
    $table->string('value');
    $table->foreignId('attribute_id')->constrained();
    $table->uuidMorphs('model');
    $table->timestamps();
});

模型有属性

Schema::create('model_has_attributes', function (Blueprint $table) {
    $table->id();
    $table->foreignId('attribute_id')->constrained();
    $table->foreignId('attribute_value_id')->constrained();
    $table->uuidMorphs('model');
    $table->timestamps();
});

现在这里所有的属性都将只使用 Attribute 模型创建,属性的值将使用所有其他模型创建,例如在我的例子中是 Category 模型。

Category类里面有这样的关系方法:

public function attributeValues()
{
    return $this->morphMany(AttributeValue::class, 'model');
}

问题:我如何才能在 Category 模型中获取所有具有值的属性,您认为我的关系是正确的还是可以更好?

【问题讨论】:

    标签: database laravel eloquent relationship laravel-7.x


    【解决方案1】:

    为什么不只是:

    Schema::create('attributes', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('value');
        $table->timestamps();
        $table->unsignedBigInteger('attributable_id');
        $table->string('attributable_type');
    });
    

    然后在属性模型中:

    class Attribute extends Model
    {
        public function attributable()
        {
            return $this->morphTo();
        }
    }
    

    在类别模型中:

    class Category extends Model
    {
        public function attributes()
        {
            return $this->morphMany(Attribute::class, 'attributable');
        }
    }
    

    编辑:

    如果你真的希望每个属性有一对多的属性值:

    在迁移中:

    Schema::create('attributes', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->timestamps();
        $table->unsignedBigInteger('attributable_id');
        $table->string('attributable_type');
    });
    
    Schema::create('attribute_values', function (Blueprint $table) {
        $table->id();
        $table->string('value');
        $table->timestamps();
        $table->unsignedBigInteger('attribute_id');
        
        $table->foreign('attribute_id')
              ->references('id')
              ->on('attributes')
    });
    

    然后在属性类中:

        public function values()
        {
            return $this->hasMany(AttributeValue::class);
        }
    

    和 AttributeValue 类:

    class AttributeValue extend Model
    {
        public function attribute()
        {
            return $this->belongsTo(Attribute:class);
        }
    }
    

    现在我不记得 hasManyThrough 是否适用于多态关系,但是:

    class Category extends Model
    {
        public function attributes()
        {
            return $this->morphMany(Attribute::class, 'attributable');
        }
    
        public function attributeValues()
        {
            return $this->hasManyThrough(AttributeValue::class, Attribute::class)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2017-07-07
      • 2021-03-10
      • 2018-08-01
      • 2017-08-14
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      • 2020-06-19
      相关资源
      最近更新 更多