如何在 Laravel 中创建枢轴 table
How to create a Pivot table in Laravel
我正在创建一个赞助应用程序,并试图在我的用户和孩子之间建立一个支点 table,但是当我有一个用户赞助一个孩子时,支点 table kid_user
未填充 kid_id
或 user_id
。不确定我错过了什么。
- 一个孩子可以拥有尽可能多的用户(赞助商),因为他们的插槽允许他们。
- 用户可以赞助一个孩子、多个孩子或同一个孩子多次
如果需要的话。
这是我的 kid_user
支点 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']);
});
}
然后在用户模型上:用户可以有很多孩子...
public function kid()
{
return $this->hasMany(Kid::class);
}
然后在Kid模型上:孩子们可以有很多用户...
public function user() {
//return $this->belongsTo(User::class); (Tried this..)
return $this->hasMany(User::class);
}
这是我正在使用的 tables。(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
枢轴 table 中。任何帮助将不胜感激。
首先,您可能想将模型中的函数重命名为复数形式,因为它不是一个而是多个关系。
所以在你的用户模型中添加:
public function kids()
{
return $this->belongsToMany(Kid::class);
}
在您的 Kid 模型中:
public function users()
{
return $this->belongsToMany(User::class);
}
然后为了保存到数据透视表 table 因为你的 table 命名是正确的,只需做:
$user->kids()->attach($kid);
将其正确保存在数据透视表中 table。首先确保您有现有的 User 和 Kid 作为变量。更多详情 here
我正在创建一个赞助应用程序,并试图在我的用户和孩子之间建立一个支点 table,但是当我有一个用户赞助一个孩子时,支点 table kid_user
未填充 kid_id
或 user_id
。不确定我错过了什么。
- 一个孩子可以拥有尽可能多的用户(赞助商),因为他们的插槽允许他们。
- 用户可以赞助一个孩子、多个孩子或同一个孩子多次 如果需要的话。
这是我的 kid_user
支点 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']);
});
}
然后在用户模型上:用户可以有很多孩子...
public function kid()
{
return $this->hasMany(Kid::class);
}
然后在Kid模型上:孩子们可以有很多用户...
public function user() {
//return $this->belongsTo(User::class); (Tried this..)
return $this->hasMany(User::class);
}
这是我正在使用的 tables。(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
枢轴 table 中。任何帮助将不胜感激。
首先,您可能想将模型中的函数重命名为复数形式,因为它不是一个而是多个关系。
所以在你的用户模型中添加:
public function kids()
{
return $this->belongsToMany(Kid::class);
}
在您的 Kid 模型中:
public function users()
{
return $this->belongsToMany(User::class);
}
然后为了保存到数据透视表 table 因为你的 table 命名是正确的,只需做:
$user->kids()->attach($kid);
将其正确保存在数据透视表中 table。首先确保您有现有的 User 和 Kid 作为变量。更多详情 here