简体   繁体   English

hasManyThrough关系问题

[英]Issue with hasManyThrough relationship

I created four models: Item, ItemOption, ItemSize, ItemColor. 我创建了四个模型:Item,ItemOption,ItemSize,ItemColor。 My intention is to create an online shop, I want to have an online shop, say I create an article shirt, then I can add many variables (options) of that same shirt, particulary colors, sizes... each option with it's own stock. 我的目的是创建一个网上商店,我想有一个网上商店,说我创建一件文章衬衫,然后我可以添加相同衬衫的许多变量(选项),特别是颜色,尺寸...每个选项都有它自己的股票。 I set these models with a hasManyThrough relationship but then I get this error: 我使用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))

These are my migrations: 这些是我的迁移:

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();
        });

And my models: 我的模特:

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);
    }


}

Try with 试试吧

on item_colors . item_colors id = item_options . id = item_options item_color_id

Wow, it is my code from previous question. 哇,这是我之前提问的代码。 It wasn't correct at 100%. 这是不正确的100%。

You should inverse some params in relation methods: 你应该在关系方法中反转一些参数:

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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM