SQLSTATE[HY000]: General error: 1364 Field 'firstname' doesn't have a default value (SQL: insert into `users` (`email`) values (test@test.com))
SQLSTATE[HY000]: General error: 1364 Field 'firstname' doesn't have a default value (SQL: insert into `users` (`email`) values (test@test.com))
我是 Laravel 框架的新手。
我遇到了一个问题,将用户 table.I 的迁移添加到我的 table 的 'firstname' 中可以为 null,这有效但对“lastname”列显示相同的错误。我已将 nullable() 添加到每一列,这不是正确的方法。这个怎么解决??谁能推荐一下。
用户迁移 table,面临 'firstname' 列的问题。
create_user_table.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('firstname');
$table->string('lastname');
$table->string('email')->unique();
$table->date('dob');
$table->char('address',100);
$table->bigInteger('phonenumber');
});
}
带商店的用户控制器
UserController.php
public function store(Request $request)
{
$request->validate([
'firstname'=>'required',
'lastname'=>'required',
'email'=>'required',
]);
$user=new User([
$user->firstname=>$request->get('firstname'),
'lastname'=>$request->get('lastname'),
'email'=>$request->get('email'),
'dob'=>$request->get('dob'),
'address'=>$request->get('address'),
'phonenumber'=>$request->get('phonenumber')
]);
$user->save();
return redirect('/users')->with('success','user added');
}
您发布的代码有错误,
这是修复:
public function store(Request $request)
{
$request->validate([
'firstname'=>'required',
'lastname'=>'required',
'email'=>'required',
]);
$user=new User([
'firstname' => $request->get('firstname'), // $user->firstname=>$request->get('firstname'),
'lastname'=> $request->get('lastname'),
'email' => $request->get('email'),
'dob' => $request->get('dob'),
'address' => $request->get('address'),
'phonenumber' => $request->get('phonenumber')
]);
$user->save();
return redirect('/users')->with('success','user added');
}
这里您还使用了批量分配,因此您应该将所有使用的列添加到用户模型中的可填充数组中。
最简单的方法是:
public function store(Request $request)
{
$request->validate([
'firstname'=>'required',
'lastname'=>'required',
'email'=>'required',
]);
$user=new User();
$user->firstname = $request->firstname;
$user->lastname = $request->lastname;
$user->email = $request->email;
$user->dob = $request->dob;
$user->address = $request->address;
$user->phonenumber = $request->phonenumber;
$user->save();
return redirect('/users')->with('success','user added');
}
同样在您的模型中,您必须添加此行以进行质量分配
protected $fillable = ['firstname','lastname','email','dob','address','phonenumber'];
您可以查看这个 link 进行批量分配:https://laravel.com/docs/7.x/eloquent#mass-assignment
我是 Laravel 框架的新手。
我遇到了一个问题,将用户 table.I 的迁移添加到我的 table 的 'firstname' 中可以为 null,这有效但对“lastname”列显示相同的错误。我已将 nullable() 添加到每一列,这不是正确的方法。这个怎么解决??谁能推荐一下。
用户迁移 table,面临 'firstname' 列的问题。 create_user_table.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('firstname');
$table->string('lastname');
$table->string('email')->unique();
$table->date('dob');
$table->char('address',100);
$table->bigInteger('phonenumber');
});
}
带商店的用户控制器 UserController.php
public function store(Request $request)
{
$request->validate([
'firstname'=>'required',
'lastname'=>'required',
'email'=>'required',
]);
$user=new User([
$user->firstname=>$request->get('firstname'),
'lastname'=>$request->get('lastname'),
'email'=>$request->get('email'),
'dob'=>$request->get('dob'),
'address'=>$request->get('address'),
'phonenumber'=>$request->get('phonenumber')
]);
$user->save();
return redirect('/users')->with('success','user added');
}
您发布的代码有错误, 这是修复:
public function store(Request $request)
{
$request->validate([
'firstname'=>'required',
'lastname'=>'required',
'email'=>'required',
]);
$user=new User([
'firstname' => $request->get('firstname'), // $user->firstname=>$request->get('firstname'),
'lastname'=> $request->get('lastname'),
'email' => $request->get('email'),
'dob' => $request->get('dob'),
'address' => $request->get('address'),
'phonenumber' => $request->get('phonenumber')
]);
$user->save();
return redirect('/users')->with('success','user added');
}
这里您还使用了批量分配,因此您应该将所有使用的列添加到用户模型中的可填充数组中。
最简单的方法是:
public function store(Request $request)
{
$request->validate([
'firstname'=>'required',
'lastname'=>'required',
'email'=>'required',
]);
$user=new User();
$user->firstname = $request->firstname;
$user->lastname = $request->lastname;
$user->email = $request->email;
$user->dob = $request->dob;
$user->address = $request->address;
$user->phonenumber = $request->phonenumber;
$user->save();
return redirect('/users')->with('success','user added');
}
同样在您的模型中,您必须添加此行以进行质量分配
protected $fillable = ['firstname','lastname','email','dob','address','phonenumber'];
您可以查看这个 link 进行批量分配:https://laravel.com/docs/7.x/eloquent#mass-assignment