Laravel 5.6 - 雄辩的创造不会接受阵列
原标题:Laravel 5.6 - eloquent create won t accept array
For some reason the create method won t accept the passed array when I try to create new user.
I am getting following error:
SQLSTATE[HY000]: General error: 1364 Field first_name doesn t have a default value (SQL: insert into `users` (`updated_at`, `created_at`) values (2018-05-30 13:24:33, 2018-05-30 13:24:33))
This is the current code:
public function storeUser($data, $request)
{
if($request->hasFile( avatar ))
$data[ avatar ] = $this->uploadAvatar($request->file( avatar ));
$password = Hash::make($data[ temp_password ]);
$data[ password ] = $password;
$data[ temporary_password ] = $password;
$data[ social ] = Strings::arrayToJson($data[ social ]);
// Assign the clinic_id from the select menu (only super admin have the permission to do this)
if(Auth::user()->hasRole( super_admin ) && isset($data[ clinic_user ]))
$data[ clinic_id ] = (int) $data[ clinic_user ];
// If the admin of the clinic is creating the user we wiil use his clinic_id for the new user
if(Auth::user()->hasRole( admin ))
$data[ clinic_id ] = Auth::user()->clinic_id;
if($this->create($data)){
event(new NewUser($this, $data[ temp_password ]));
if(Auth::user()->hasRole( super_admin ) && isset($data[ clinic_owner ]))
event(new ClinicUpdate($this));
if(Auth::user()->hasRole( super_admin , admin ) && Auth::user()->id !== $this->id)
$user->giveRole($data[ user_role ]);
return true;
}
return false;
}
User model:
belongsTo(Clinic::class);
}
public function files()
{
return $this->hasMany( App/Media );
}
public function verifyUser()
{
return $this->hasOne( AppVerifyUser );
}
}
UserCreateRequest:
hasRole( super_admin , admin ))
$rules = [
user_role => [Rule::in([ super_admin , admin , user ])],
clinic_owner => integer ,
clinic_user => integer ,
];
return array_merge($rules, [
first_name => required|string|max:255 ,
last_name => required|string|max:255 ,
email => "required|string|email|unique:users",
temp_password => required|string|min:8 ,
title => integer ,
gender => [Rule::in([0, 1])],
position => string|max:255|nullable ,
phone => string|max:255|nullable ,
location => string|max:255|nullable ,
about => string|nullable ,
social.* => url|nullable ,
avatar => nullable|image|mimes:jpeg,jpg,png,gif ,
]);
}
public function attributes()
{
return [
social.facebook => Facebook ,
social.twitter => Twitter ,
social.instagram => Instagram ,
social.linkedin => Linkedin ,
temp_password => Temporary Password ,
];
}
}
NewUser event:
_user = $user;
$this->_tempPass = $tempPass;
}
/**
* Get the channels the event should broadcast on.
*
* @return IlluminateBroadcastingChannel|array
*/
public function broadcastOn()
{
return [];
}
}
UsersController (the store method):
public function store(UserCreateRequest $request)
{
$validated = $request->validated();
$model = new UserQuery;
if(!$model->storeUser(XSS::clean($validated, [ avatar ]), $request)){
Session::flash( alert , [
message => Something went wrong. Please try again ,
type => danger
]);
return Redirect::back();
}
return redirect( /admin/users );
}
This the dd for the data:
array:17 [▼
"user_role" => "admin"
"clinic_user" => "1"
"first_name" => "Sasha"
"last_name" => "Miljkovic"
"email" => "xxxxxx@xxxxx.com"
"temp_password" => "12345678"
"title" => "10"
"gender" => "0"
"position" => "Tester"
"phone" => "1234"
"location" => "Pirot"
"about" => "Maecenas gravida tellus augue, sed mollis quam viverra at. Aenean sit amet dui non eros laoreet porta et nec nisi. Cras lectus justo, porttitor quis mattis nec, ▶"
"social" => "[]"
"avatar" => "avatar_5b0ea46d2af8d.jpg"
"password" => "$2y$10$gxGPBbwS44KHXLn57leRpukX/zu/rX3SSn7jRdM27kvQb9N84CcGa"
"temporary_password" => "$2y$10$gxGPBbwS44KHXLn57leRpukX/zu/rX3SSn7jRdM27kvQb9N84CcGa"
"clinic_id" => 1
]
最佳回答
you must pass all required fileds
$data[ first_name ] = some name ;
the error tell you that the first_name field does not pass you must pass this field and other field are required to prevent this error
问题回答
You should instantiate User instead, try :
(new User())->create($data)
If it works replace :
$model = new UserQuery;
By :
$model = new User;