我正在构建一个用户管理系统。。。
当我创建一个用户并将图像放入"头像字段"时,用户将成功创建
但当我让"头像字段"为空时,他会向我显示代币和我插入字段的数据
我已经在我的表单中放入了@csrf
{
"_token":"f8UnQweYC4xR6WahdsUDlFGoGxyuyzm61rpBEXXXX",
"_method":"post",
"email":"example@abc.xyz",
"name":"example",
"password":"123456789"
}
存储方法
public function store(Request $request)
{
$this->validate($request, array(
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:8',
));
$password = Hash::make($request->password);
$user = new User;
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = $password;
$user->roles()->detach();
if ($request['user']) {
$user->roles()->attach(Role::where('name','User')->first());
}
if ($request['editor']) {
$user->roles()->attach(Role::where('name','Editor')->first());
}
if ($request['admin']) {
$user->roles()->attach(Role::where('name','Admin')->first());
}
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 300)->save( public_path('/images/avatars/' . $filename ) );
} else {
return $request;
$user->avatar = '';
}
$user->save();
return view('content.admin.user.index');
}
当您在没有上传图像的情况下返回$request
时,它会返回$request
中的全部数据。
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 300)->save( public_path('/images/avatars/' . $filename ) );
} else {
$user->avatar = '';
}