如何在Laravel中创建数据透视表 [英] How to create a Pivot table in Laravel

查看:100
本文介绍了如何在Laravel中创建数据透视表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个赞助应用程序,并试图使数据透视表在我的用户和孩子之间运行,但是当我有一个用户赞助儿童时,数据透视表kid_user不会填充kid_iduser_id.不知道我在想什么.

I have a sponsorship app I'm creating and trying to get a pivot table working between my users and kids, but when I have a user sponsor a kid, the pivot table kid_user is not populated with the kid_id or the user_id. Not sure what I'm missing.

  • 孩子可以拥有尽可能多的用户(赞助商).
  • 用户可以多次赞助一个孩子,多个孩子或同一个孩子 如果需要的话.
  • A Kid can have as many users (sponsors) as their slots allow them.
  • Users can sponsor a kid, multiple kids or the same kid multiple times if needed.

这是我的kid_user数据透视表:

Here is my kid_user pivot table:

public function up()
{
    Schema::create('kid_user', function (Blueprint $table) {
        $table->integer('kid_id')->unsigned()->index();
        $table->foreign('kid_id')->references('id')->on('kids')->onDelete('cascade');
        $table->integer('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->primary(['kid_id', 'user_id']);
    });
}

然后在用户模型上:用户可以有很多孩子...

Then on the User model: The user can have many kids...

public function kid()
{
    return $this->hasMany(Kid::class);
}

然后在Kid模型上:孩子可以有很多用户...

Then on the Kid model: The kids can have many users...

  public function user() {
      //return $this->belongsTo(User::class); (Tried this..)
        return $this->hasMany(User::class);
  }

这是我正在使用的表格.(表格中仅包含相关信息)

Here are the tables I'm using. (Only relevant info is included in the tables)

+---------------------+
| Tables_in_Database  |
+---------------------+
| kid_user            |
| kids                |
| users               |
+---------------------+

+-----------------------------+
|        users table          |
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
|  1 | John       | Smith     |
|  2 | Jane       | Doe       |
+----+------------+-----------+

+-----------------------------+
|        kids table           |
+----+------------+-----------+-------+
| id | first_name | last_name | slots |
+----+------------+-----------+-------+
|  1 | Bobby      | Little    |   3   | -> Can be sponsored 3 times
+----+------------+-----------+-------+

+--------------------+
|   kid_user table   | THE BELOW RESULTS ARE WHAT I'M LOOKING FOR.
+--------------------+
| *kid_id | *user_id | * = Primary Key
+---------+----------+
|    1    |     1    | -> Sponsored 1st Slot
+---------+----------+
|    1    |     2    | -> Sponsored 2nd Slot
+---------+----------+
|    1    |     1    | -> Sponsored 3rd Slot
+---------+----------+

因此,当用户赞助孩子时.我想要kid_id& user_id要输入到kid_user数据透视表中.任何帮助将不胜感激.

So when a User sponsors a Kid. I would like the kid_id & user_id to be entered into the kid_user pivot table. Any help would be appreciated.

推荐答案

首先,您可能想对模型中的函数进行重命名,因为它不包含多个关系.

First of all you might wanna rename the functions in your models in plural as it does not have one but many from the relationship.

因此,在您的用户模型中添加以下内容:

So in your user model add this:

public function kids()
{
    return $this->belongsToMany(Kid::class);
}

在您的Kid模型中:

public function users()
{
     return $this->belongsToMany(User::class);
}

然后要保存到数据透视表,因为您的表命名正确,只需执行以下操作:

Then in order to save to the pivot table since your table naming is correct, just doing:

$user->kids()->attach($kid);

将其正确保存在数据透视表中.首先确保您已有变量的User和Kid. 此处

Will save it properly in the pivot table. Making sure first that you have existing User and Kid for the variables. More details here

这篇关于如何在Laravel中创建数据透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆