laravel - 如何在 Laravel 迁移中创建连接表?
问题描述
我有一个包含以下列的用户表,用户模型与手机模型具有一对多的关系。
create_users_table.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
然后我有一个电话表,它有一个外键 user_id create_phones_table.php
public function up()
{
Schema::create('phones', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->unsigned();
$table->string('phone');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
}
我的用户模型
<?php
namespace App;
use App\Phone;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* Define Relationship
*/
public function phones()
{
return $this->hasMany(Phone::class);
}
}
我的手机型号
<?php
namespace App;
use App\User;
use Illuminate\Database\Eloquent\Model;
class Phone extends Model
{
protected $table = 'phones';
protected $fillable = ['phone' , 'user_id'];
public function user()
{
return $this->belongsTo(User::class);
}
}
我想在我的数据库调用 phone_user_table.php 中有第三个表。像一个数据透视表,我加入了用户表和电话表,我可以在其中查看所有记录。这是我尝试加入表格的代码。
create_phone_user.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePhoneUser extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('phone_user', function (Blueprint $table) {
$table->unsignedBigInteger('user_id')->unsigned();
$table->unsignedBigInteger('phone_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('phone_id')->references('id')->on('phones')->onDelete('cascade');
$table->timestamps();
$table
->join('users', 'users.id', '=', 'phone_user.user_id')
->join('phones', 'phones.id', '=', 'phone_user.phone_id')
->select('phone_user.*', 'users.name', 'phones.phone')
->get();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('phone_user');
}
}
但是,这似乎是在给 Base Table 电话已经存在。
感谢提供的所有帮助。
谢谢你。
解决方案
这就是它基于Laravel 多对多关系文档的工作方式。
第一的
您需要创建 2 个model
您想要建立关系的对象。
在您的示例中,您有USER和PHONE关系。
在您的用户模型中,您需要像这样声明关系:
public function phones() {
return $this->belongsToMany(Phone::Class);
}
在您的手机型号中,您可以这样做:
public function users() {
return $this->belongsToMany(User::Class);
}
第二
您需要有 3migration
个用于user
,phone
还有phone_user
. 所以它应该是这样的。
电话用户迁移
Schema::create('phone_user', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('phone_id')->index();
$table->unsignedBigInteger('user_id')->index();
$table->timestamps();
$table->foreign('phone_id')->references('id')->on('phones')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
注意:您不需要在两者中都有 unsignedBigInteger
user
和phone
migration
。
当您有 auser list
和 a时phone list
,您现在可以将其分配phone
给user
如下:
控制器
$user = User::find(1);
$phone = Phone::find(1);
$user->phones()->attach($phone);
推荐阅读
- html - 在图像下方放置文本
- node.js - 解耦授权实现
- python - 数据框填充子集
- node.js - 没有按名称查找频道
- asp.net-core - 我们可以使用 Visualbasic.PowerPacks.Vs 和 ASP.NET Core 将文本发送到打印机吗?
- python - Pandas 使用cols 并从多个数据帧追加
- javascript - Recursive generator in javascript
- python - 如何使用 Tensorboard 检查训练模型的准确性?
- flutter - Flutter:使用 SizeTransition 从下到上绘制容器
- ios - 出现此错误的原因是什么:“NSIndexPath' 不能隐式转换为 'IndexPath';您的意思是使用 'as' 显式转换吗?”