外键是一个字段,用于通过主键建立两个表之间的关系(你也可以使用一个非主键字段,但不建议使用)。
在本教程中,我展示了如何在Laravel 8项目中使用迁移创建表时添加外键约束。

内容
1.数 据库配置
打开.env 文件。
指定主机、数据库名称、用户名和密码。
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tutorial
DB_USERNAME=root
DB_PASSWORD=
2.创 建表并添加外键
使用迁移创建countries,states, 和cities 表。
我正在为states 和cities 表添加外键。
states表被链接到 表,并且countriescities表与 表相连接。states
- 创建
Countries表 -
php artisan make:migration create_countries_table
- 现在,从项目根目录导航到
database/migration/文件夹。 - 找到一个以
create_countries_table结尾的PHP文件并打开它。 - 在
up()方法中定义表的结构。
public function up()
{
Schema::create('countries', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
- 创建
States表 -
php artisan make:migration create_states_table
- 同样地,在
database/migration/文件夹中找到一个以create_states_table结尾的PHP文件并打开。 - 在
up()方法中定义表的结构。
添加外键 -
- 我正在为
country_id字段添加外键。 - 设置这个字段的数据类型为UNSIGNED BIGINT -
$table->unsignedBigInteger('country_id');。数据类型必须是UNSIGNED,并且与父表的链接字段数据类型相同。 - 这里,
countries表id字段的数据类型是biginteger。 - 添加外键 -
$table->foreign('country_id')
->references('id')->on('countries')->onDelete('cascade');
值 -
- foreign() -传递你想用外键约束的字段名。
- references() -传递连接表的字段名。
- on() -连接表的名称。
- onDelete('cascade') -启用删除附加数据。
public function up()
{
Schema::create('states', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('country_id');
$table->string('name');
$table->timestamps();
$table->foreign('country_id')
->references('id')->on('countries')->onDelete('cascade');
});
}
- 创建
Cities表 -
php artisan make:migration create_cities_table
- 同样,在
database/migration/文件夹中找到一个以create_cities_table结尾的PHP文件并打开它。 - 在
up()方法中定义表的结构。
添加外键 -
- 我正在为
states_id字段添加外键。 - 设置这个字段的数据类型为UNSIGNED BIGINT -
$table->unsignedBigInteger('state_id');。数据类型必须是UNSIGNED,并且与父表的链接字段数据类型相同。 - 这里,
states表id字段的数据类型为biginteger。 - 添加外键 -
$table->foreign('state_id')
->references('id')->on('states')->onDelete('cascade');
值 -
- foreign() -传递你想用外键约束的字段名。
- references() -传递连接表的字段名。
- on() -连接表的名称。
- onDelete('cascade') -启用删除附件数据的功能。
public function up()
{
Schema::create('cities', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('state_id');
$table->string('name');
$table->foreign('state_id')
->references('id')->on('states')->onDelete('cascade');
$table->timestamps();
});
}
- 运行迁移以创建表 -
php artisan migrate
3.模 型
创建国家、州和城市模型。
- 创建
Countries模型。
php artisan make:model Countries
- 打开
app/Models/Countries.php文件。 - 指定大量可分配的模型属性 -
name使用$filliable属性。
完成的代码
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Countries extends Model
{
use HasFactory;
protected $fillable = [
'name'
];
}
- 创建
States模型:
php artisan make:model States
- 打开
app/Models/States.php文件。 - 指定大量可分配的模型属性 -
country_id,和使用$filliable属性的name。
完成的代码
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class States extends Model
{
use HasFactory;
protected $fillable = [
'country_id','name'
];
}
- 创建
Cities模型:
php artisan make:modelCities
- 打开
app/Models/Cities.php文件。 - 指定大量可分配的模型属性 -
state_id, 和使用$filliable属性的name。
完成的代码
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Cities extends Model
{
use HasFactory;
protected $fillable = [
'state_id','name'
];
}
4.总 结
在这个例子中,我在一个表上添加了一个外键,但你可以按照同样的步骤在一个表上添加一个以上的外键。
你可以从这里了解到更多信息。