gpt4 book ai didi

php - 照亮\数据库\QueryException : SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `users`

转载 作者:行者123 更新时间:2023-12-05 03:56:19 29 4
gpt4 key购买 nike

我有一个问题,但我得到的答案并没有真正帮助我。

所以我一直在尝试为每个用户基本上创建一个 Profiles 表。我遵循了一对一关系的规则,但我不断收到此错误

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into users (name, email, password, admin, updated_at, created_at) values (Darlington Okorie, darlingtonokoriec@gmail.com, $2y$10$Ob161LW8sbbv5uDv9VVbienlmW.DWXVDK3wdfC0I0NlnOrg1Jx/a2, 1, 2019-12-23 17:41:30, 2019-12-23 17:41:30))

这是我在 UsersTableSeeder

中编写的代码
public function run()
{

$user = App\User::create([
'name' => 'Darlington Okorie',
'email' => 'darlingtonokoriec@gmail.com',
'password' => bcrypt('password'),
'admin' => 1
]);

App/Profile::create([
'user_id' => $user->id,
'avatar' => 'uploads/avatar/img.png',
'about' => 'Great at writing. Better at blogging',
'facebook' => 'facebook.com',
'youtube' => 'youtube.com'
]);
}

而且我还在我的 create_users_table 中定义了 user_id

public function up()
{
Schema::create('users', function (Blueprint $table) {
Schema::dropIfExists('users');
$table->bigIncrements('id');
$table->string('name');
$table->string('user_id');
$table->string('email')->unique();
$table->boolean('admin')->default(0);
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}

并在我的 create_profiles_table 中定义了 user_id

 public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('avatar');
$table->integer('user_id');
$table->text('about');
$table->string('facebook');
$table->string('youtube');

$table->timestamps();
});
}

有什么问题吗?

最佳答案

我也遇到过这种问题。我的案例,我忘了在 fillable 中写这个字段。

protected $fillable = [
'name',
];

在$fillable 中写入字段后。我的问题已解决。

关于php - 照亮\数据库\QueryException : SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `users` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59459471/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com