Laravel API 默认驱动为 token,可查看config/auth.php。
'api' => [
'driver' => 'token',
'provider' => 'users',
],
我们来看看如何在项目中使用吧
1. 先给数据库中加一个字段:
1.1 项目下执行命令 php artisan make:migration update_users_table_add_api_token --table=users
生成迁移文件.
1.2 然后修改迁移文件
Schema::table('users', function (Blueprint $table) {
$table->string('api_token', 64)->after('password')
->unique()
->nullable()
->default(null);
});
1.3 然后修改模型
添加 api_token
到 User
模型 $fillable
属性
protected $fillable = [
'name', 'email', 'password','api_token',
];
2. 用户注册时生成api_token
代码放在注册控制器 app/Http/