如何向多对多数据透视表添加额外的列

时间:2021-04-17 12:41:05

标签: php laravel eloquent laravel-8 eloquent-relationship

我有一个简单的 items 表。

示例:

纸。由竹子和棉花制成(如果谷歌没有说谎)。

还有一本纸做的书。

所以竹和棉的纸父母也是书的孩子。

我想我可以用两个词来解释我的表格结构。

现在是我的代码。所有这些代码示例和表结构都尽可能地简化了。

items

╔════╦═══════════════╦══════╗
║ id ║  name         ║ price║
╠════╬═══════════════╬══════╣
║  1 ║ Book          ║ 500  ║
║  2 ║ Paper         ║  50  ║
║  3 ║ Bamboo        ║  15  ║
║  4 ║ Cotton        ║  7   ║
╚════╩═══════════════╩══════╝

item_item 表(多对多)

╔════╦═══════════════╦══════════╗
║ id ║  item_id      ║ parent_id║
╠════╬═══════════════╬══════════╣
║  1 ║     2         ║    1     ║
║  2 ║     3         ║    2     ║
║  3 ║     4         ║    2     ║
╚════╩═══════════════╩══════════╝

模型 Item

class Item extends Model
{
    protected $fillable = ["name", "price"];

    public $timestamps = false;

    public function components()
    {
        return $this->belongsToMany('App\Models\Item', "item_item", "parent_id", "item_id");
    }
}

ItemController 创建

 public function store(Request $request)
    {
        $request->validate([
            "name" => "required",
            "price" => "required",
        ]);

        $item = Item::create([
            "name" => $request->name,
            "price" => $request->price,
        ]);

        $item->components()->attach($request->items);


        return redirect()->route("items");
    }

同步更新:

$item->components()->sync($request->components);

我的观点

<div class="form-group">
    <label for="item">Choose child items</label>
    @foreach ($items as $item)
        <div class="checkbox">
            <label ><input class="mr-2" name="items[]" type="checkbox" value="{{ $item->id }}"/>{{ $item->name }}</label>
        </div>
    @endforeach
</div>

这很好用,但问题是如何将列 item_count 添加到数据透视表 item_item。计算需要创建多少项。

例如用 500 篇论文来制作一本书

我尝试在 this 视频中点赞,但不起作用。

2 个答案:

答案 0 :(得分:1)

将表名和类更改为差异名称,例如书籍、作者 然后你可以使用 book_id 和 author_id

class book {
      public function author(){
         return $this->belongsTo(App\Models\Author,'id','author_id');
      }
}

参考Laravel Eloquent: Relationships

示例:

表结构如下:

<块引用>

物品 id - 整数 价格 - 整数

<块引用>

父母 id - 整数 名称 - 字符串

<块引用>

孩子们 item_id - 整数 parent_id - 整数

模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    public function parent()
    {
        return $this->belongsTo(Child::class, 'id', 'item_id');
    }
}

建议

<块引用>

你可以在 items 表中使用 part_id

Schema::create('items', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->integer('parnt_id');
    $table->integer('price');
    $table->timestamps();
});

答案 1 :(得分:1)

我假设您可以创建一个迁移来更新您的数据透视表结构。

然后,您可以更改您的

$item->components()->attach($request->items);

$item->components()->attach($request->items, [item_count]);

在文档中

将关系附加到模型时,您还可以传递要插入中间表的附加数据数组:

$user->roles()->attach($roleId, ['expires' => $expires]);

同步为

$item->components()->sync([$request->items, $item_count]);

在文档中

您还可以使用 ID 传递额外的中间表值:

$user->roles()->sync([1 => ['expires' => true], 2, 3]);