Проблема с отношением hasManyThrough - PullRequest
0 голосов
/ 30 мая 2019

Я создал четыре модели: 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 голосов
/ 30 мая 2019

Ух ты, это мой код из предыдущего вопроса. Это не было правильно на 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');
}
0 голосов
/ 30 мая 2019

Попробуйте с

на item_colors. id = item_options. item_color_id

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...