【问题标题】:How to add extra column to pivot table many to many如何向多对多数据透视表添加额外的列
【发布时间】:2021-07-12 06:01:47
【问题描述】:

我有简单的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 视频中做类似的事情,但没有成功。

【问题讨论】:

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


    【解决方案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 表中使用 parnt_id

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

    【讨论】:

      【解决方案2】:

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

      然后,你可以改变你的

      $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]);
      

      【讨论】:

      • 这不是我想要的。
      • @Spectr 那你想要什么?您只想要数据透视表中的自定义列,而不是主键?
      • 请仔细阅读问题问题是如何将列 item_count 添加到数据透视表 item_item。
      • @Spectr 我已经更新了我的答案,试试这个。
      猜你喜欢
      • 2022-01-16
      • 2019-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 1970-01-01
      • 2018-06-27
      • 2023-04-08
      相关资源
      最近更新 更多