2 回答
TA贡献1883条经验 获得超3个赞
实现这一目标的最简单方法是:
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'];
TA贡献1808条经验 获得超4个赞
您发布的代码中存在错误,这是修复方法:
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');
}
同样在这里,您正在使用质量分配,因此您应该将所有使用的列添加到用户模型中的可填充数组中。
- 2 回答
- 0 关注
- 164 浏览
添加回答
举报