【问题标题】:Issue with shirt -> colors -> sizes laravel relationship衬衫问题 -> 颜色 -> 尺寸 laravel 关系
【发布时间】:2019-05-22 12:35:03
【问题描述】:

我正在努力解决这种我认为是多对多的关系,我有 3 个模型,一个衬衫模型,一个衬衫颜色模型和一个衬衫尺寸模型。现在这就是我看待事物的方式,我可以说:

一件衬衫可以有多种颜色和多种尺码,但您也可以说一件衬衫只有一种颜色和一种尺码(一件衬衫不能同时有多种颜色/尺码......),所以这让我很困惑.我觉得这必须是衬衫、尺寸和颜色之间的多对多关系,但我不太确定如何(如果我应该)做到这一点?

这是我的迁移:

Schema::create('shirts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('slug')->unique();
            $table->string('url')->unique();
            $table->string('title')->unique();
            $table->longText('body');
            $table->decimal('finalPrice', 5,2);
            $table->integer('totalCount');
            $table->string('image')->nullable();
            $table->boolean('isVisible')->default(false);
            $table->boolean('beenPublished')->default(false);
            $table->boolean('scheduleForMail')->default(false);
            $table->timestamps();
        });


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



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

【问题讨论】:

  • 是否会出现尺寸/颜色组合不可用的情况? IE。 SM/Blue、SM/Red 和 MD/Blue 都有,但 MD/Red 不行? (或类似的东西)
  • 是的,我想是的,每件衬衫也会有一个 stock left 属性。
  • 好的。然后(就个人而言),我将为shirts(想想产品)提供一个表,为shirt_variations 提供一个表。在那张桌子上,我有colour(或colour_id,如果你想将颜色存储在单独的表格中),size(或size_id)也是如此。您也可以将不同的价格/库存附加到该表的列上,并且只为每个变体包含一行。有人可能有更好的解决方案,但这是我的想法。不过,这个问题可能属于“主要基于意见”的关闭原因。

标签: php laravel


【解决方案1】:

创建表“shirt_size_colors”

php artisan make:migration create_shirt_size_colors_table

迁移中:

Schema::create('shirt_size_colors', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('shirt_id');
            $table->foreign('shirt_id')->references('id')->on('shirts')->onDelete('cascade');
            $table->unsignedBigInteger('color_id');
            $table->foreign('color_id')->references('id')->on('shirtcolors')->onDelete('cascade');
            $table->unsignedBigInteger('size_id');
            $table->foreign('size_id')->references('id')->on('shirtsizes')->onDelete('cascade');
            $table->timestamps();
        });

根据您的问题,您可以在此处添加一件单色单码衬衫 和一件有多种颜色和尺码的衬衫。

例如:

对于一件衬衫,一种颜色和一种尺寸 衬衫 ID 1,颜色 ID 1,尺码 ID 1 然后按照下面的表格条目

-------------------------------------
shirt_id   |  color_id  |  size_id   |
-------------------------------------
    1      |      1     |      1     |
-------------------------------------

一件衬衫有多种颜色和多种尺寸 衬衫 ID 2,颜色 ID [1,2],尺码 ID [1,2] 然后按照下面的表格条目

-------------------------------------
shirt_id   |  color_id  |  size_id   |
-------------------------------------
    2      |      1     |      1     |
-------------------------------------
-------------------------------------
    2      |      1     |      2     |
-------------------------------------
------------------------------------
    2      |      2     |      1     |
-------------------------------------
-------------------------------------
    2      |      2     |      2     |
-------------------------------------

创建模型

php artisan make:model ShirtSizeColors

在 ShirtSizeColors 模型中

这里我使用 Shirt、ShirtSize、ShirtColor 型号,你可以用你的型号名称替换

protected $table = 'shirt_size_colors';

    public $timestamps = true;

    protected $fillable = ['shirt_id', 'color_id', 'size_id'];

    //for shirt data
    public function shirt(){
        return $this->belongsTo(Shirt::class, 'shirt_id');
    }

    //for color data
    public function color(){
        return $this->belongsTo(ShirtColor::class, 'color_id');
    }

    //for size data
    public function size(){
        return $this->belongsTo(ShirtSize::class, 'size_id');
    }

现在衬衫模型的关系

// for get shirt colors and sizes
    public function shirt_size_colors(){
        return $this->hasMany(ShirtSizeColors::class, 'shirt_id');
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2012-09-26
    • 2012-06-17
    • 1970-01-01
    相关资源
    最近更新 更多