hasManyThrough关系的问题

时间:2019-05-30 12:08:12

标签: php laravel

我创建了四个模型:Item,ItemOption,ItemSize,ItemColor。我的意图是创建一个网上商店,我想拥有一个网上商店,比如说我创建一件商品衬衫,然后我可以添加同一衬衫的许多变量(选项),特别是颜色,尺寸……每个选项都有自己的选择股票。我将这些模型设置为hasManyThrough关系,但随后出现此错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'item_colors.item_option_id' in 'on clause' (SQL: select `item_colors`.*, `item_options`.`item_id` as `laravel_through_key` from `item_colors` inner join `item_options` on `item_options`.`id` = `item_colors`.`item_option_id` where `item_options`.`item_id` in (1))

这些是我的迁移:

Schema::create('item_options', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('item_id')->index()->nullable();
            $table->foreign('item_id')->references('id')->on('items')->nullable();
            $table->unsignedInteger('item_size_id')->index()->nullable();
            $table->foreign('item_size_id')->references('id')->on('item_sizes')->nullable();
            $table->unsignedInteger('item_color_id')->index()->nullable();
            $table->foreign('item_color_id')->references('id')->on('item_colors')->nullable();
            $table->timestamps();
        });


Schema::create('items', function (Blueprint $table) {
            $table->increments('id');
            $table->string('image')->nullable();
            $table->string('title');
            $table->decimal('finalPrice', 5,2);
            $table->text('body');
            $table->timestamps();
        });

Schema::create('item_colors', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('colorCode');
            $table->timestamps();
        });

Schema::create('item_sizes', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->timestamps();
        });

还有我的模特:

class Item extends Model
{


    protected $table = 'items';


    public function options()
    {
        return $this->hasMany(ItemOption::class);
    }

    public function sizes()
    {
        return $this->hasManyThrough(ItemSize::class, ItemOption::class);
    }

    public function colors()
    {
        return $this->hasManyThrough(ItemColor::class, ItemOption::class);
    }


}


class ItemOption extends Model
{

    protected $fillable = ['item_id', 'item_color_id', 'item_size_id', 'stock'];

    public function color()
    {
        return $this->belongsTo(ItemColor::class);
    }

    public function size()
    {
        return $this->belongsTo(ItemSize::class);
    }


}

2 个答案:

答案 0 :(得分:0)

尝试

item_colors上。id = item_optionsitem_color_id

答案 1 :(得分:0)

哇,这是我上一个问题的代码。并非100%正确。

您应该在关联方法中逆转一些参数:

public function sizes()
{
    return $this->hasManyThrough(ItemSize::class, ItemOption::class, 'item_id', 'id', 'id', 'item_size_id');
}

public function colors()
{
    return $this->hasManyThrough(ItemColor::class, ItemOption::class, 'item_id', 'id', 'id', 'item_color_id');
}