簡體   English   中英

Laravel Eloquent 3 個表 + 1 個 pivot 表

[英]Laravel Eloquent 3 tables + 1 pivot table

我正在設計一個數據庫來管理products 每個產品可能有多種color和多種size (例如,如果產品是 T 恤,那么同一件襯衫可以是小號、中號或大號,並且可以是紅色、綠色或藍色)所以我最終得到了 3 張桌子。

  • 顏色:身份證、姓名
  • 尺寸:身份證、姓名
  • 產品:id,名稱

我需要能夠存儲庫存中有多少具有特定尺寸和特定顏色的產品以及每種產品的價格。 所以我創建了一個包含以下字段的第四個表:

  • color_id, size_id, product_id, 數量, 價格

我為每個表都有一個 model 並定義它們之間的關系,我使用了belongsToMany方法。 “問題”是它只允許指定 2 個表的外鍵(文檔的示例是用戶、角色和角色用戶)。

有沒有辦法在 Eloquent 中定義這些表之間的這種關系?

例如,我需要能夠查詢數據庫以獲取所有可用的綠色大型產品。 我知道如何使用純 SQL 來實現這一點,但我正在尋找更多的 Eloquent 版本。

您所說的“產品”可能更好地命名為“樣式”。 產品表為pivot表有數量和價格。 是的,您有四個表: colorssizesstyles (請參閱播種機示例),以及 color_size_style pivot 表,我將其稱為products :

colors、大小和 styles 表的遷移:

// create colors table
Schema::create('colors', function (Blueprint $table) {
    $table->id();
    $table->string('color', 100);
});

// create sizes table
Schema::create('sizes', function (Blueprint $table) {
    $table->id();
    $table->string('size', 100);
});

// create styles table
Schema::create('styles', function (Blueprint $table) {
    $table->id();
    $table->string('style', 100);
});

遷移產品表,即 color_size_style 三路 pivot 表,附加列數量和價格:

// create products table
Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->foreignId('color_id')->constrained();
    $table->foreignId('size_id')->constrained();
    $table->foreignId('style_id')->constrained();
    $table->integer('quantity')->default(0);
    $table->decimal('price')->default(0.00);
});

顏色、尺寸和樣式的模型

// define color model with size and style relationships
class Color extends Model
{
    public function sizes()
    {
        return $this->belongsToMany(Size::class, 'products');
    }
    public function styles()
    {
        return $this->belongsToMany(Style::class, 'products');
    }
}

// define size model with color and style relationships
class Size extends Model
{
    public function colors()
    {
        return $this->belongsToMany(Color::class, 'products');
    }
    public function styles()
    {
        return $this->belongsToMany(Style::class, 'products');
    }
}

// define style model with color and size relationships
class Style extends Model
{
    public function colors()
    {
        return $this->belongsToMany(Color::class, 'products');
    }
    public function sizes()
    {
        return $this->belongsToMany(Size::class, 'products');
    }
}

colors、尺寸和 styles 的播種機:

// seed default colors
class ColorSeeder extends Seeder
{
    public function run()
    {
        $colors = [
            ['color' => 'black'],
            ['color' => 'white'],
            ['color' => 'gray'],
            ['color' => 'maroon'],
            ['color' => 'purple'],
            ['color' => 'navy'],
            ['color' => 'teal']
        ];

        DB::table('colors')->insert($colors);
    }
}

// seed default sizes
class SizeSeeder extends Seeder
{
    public function run()
    {
        $sizes = [
            ['size' => 'XS'],
            ['size' => 'S'],
            ['size' => 'M'],
            ['size' => 'L'],
            ['size' => 'XL'],
            ['size' => 'XXL']
        ];

        DB::table('sizes')->insert($sizes);
    }
}

// seed default styles
class StyleSeeder extends Seeder
{
    public function run()
    {
        $styles = [
            ['style' => 'Unisex Short Sleeve T-Shirt'],
            ['style' => 'Unisex Tank Top'],
            ['style' => 'Unisex Pullover Hoodie'],
            ['style' => 'Women\'s Short Sleeve T-Shirt'],
            ['style' => 'Women\'s Flowy Long Sleeve Shirt'],
            ['style' => 'Men\'s Polo Shirt']
        ];

        DB::table('styles')->insert($styles);
    }
}

現在您可以在 products 表中添加一個條目:

$color = Color::inRandomOrder()->first();
$size = Size::inRandomOrder()->first();
$style = Style::inRandomOrder()->first();

$color->sizes()->attach($size, ['style_id' => $style->id]);

// or in any other combination:
// $color->styles()->attach($style, ['size_id' => $size->id]);
// $size->colors()->attach($color, ['style_id' => $style->id]);
// $size->styles()->attach($style, ['color_id' => $color->id]);
// $style->colors()->attach($color, ['size_id' => $size->id]);
// $style->sizes()->attach($size, ['color_id' => $color->id]);

要獲得所有可能的組合(不是我們想要的):

select colors.color,sizes.size,styles.style from colors join sizes join styles;

要獲得我們標記為存在的產品:

select products.id,products.quantity,styles.style,sizes.size,colors.color,products.price
from products
join colors on colors.id=products.color_id
join sizes on sizes.id=products.size_id
join styles on styles.id=products.style_id
order by style,size,color;

雖然技術上沒有必要,但您可能還需要 model 產品。 或者也許 controller 就足夠了——我不確定。 但這應該足以讓您了解三路 pivot 概念。 ;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM