php - Laravel API:RegisterController 在创建用户时设置默认值不起作用
问题描述
我开始从事 Laravel API 项目。我在 users 表中添加了一个列来引用帐户 ID。使用所需的可填充键更新模型后,注册用户后列值保持为空。我实际上不知道问题出在哪里。
退货也不包含新列。
用户模型
<?php
namespace App;
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'account_id',
];
/**
* 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',
];
protected $table = 'api_users';
}
注册控制器:创建函数
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'account_id' => time(),
]);
}
数据库迁移
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('api_users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('account_id', 60)->unique();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('api_users');
}
}
解决方案
您使用的外国 account_id 参考可能无效。
推荐阅读
- bash - 从 sh 脚本调用 ps1 文件
- git - 我可以将一个 Git 存储库中的两个单独的文件夹统一到一个文件夹中吗?
- c# - 在 Azure 中部署后,现在将显示某个页面
- php - 如何从 Codeigniter 中的 url 传递多个参数?
- selinux - 如何在 SLES 15-SP1 上安装 SELinux?
- node.js - Haste 模块地图中不存在模块“调度程序”
- java - 活动和意图基础
- tensorflow - Keras fit_generator 带有来自目录的图像和一个常量张量
- r - 使用 sweave 在报告中显示图形:只显示最终报告,没有中间文件
- java - 在父对象中生成子类结果的反序列化