Cant create user I have code in controller which receive from registration form data and save to users table. I cant paste all data only first three is inserted while next wont.
public function create_user(Request $request){
$data = $request->all();
$user = new \App\User;
$user::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => $data['phone'],
'city' => (isset($data['city'])) ? $data['city'] : NULL,
'image' => (isset($data['image'])) ? $data['image'] : NULL
]);
return view('admin.register');
}
have you dealt with mass assignment protection? IE, added the new fields to $fillable
btw, if running php 7, this can be simplified;
'city' => (isset($data['city'])) ? $data['city'] : NULL,
//with null coalesce operator
'city' => $data['city'] ?? NULL,
also, this is redundant
$user = new \App\User;
$user::create([
// just do
User::create([
and add use App\User at the top of the controller
Not that it is vital to how it works, you can also clean up your code a bit. (Requires use App\User; under the namespace)
public function create_user(Request $request){
User::create([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password),
'phone' => $request->phone,
'city' => (isset($request->city)) ? $request->city : NULL,
'image' => (isset($request->image)) ? $request->image : NULL
]);
//redirect etc
}
use App\User;
public function create(Request $request) {
$user = new User;
$user->name = $request->name;
$user->email = $request->email;
........
$user->save();
return redirect()->route('your_uri');
}
Thank you all. My code now work. As you advised it was mass assignment protection. Thank you for syntax recommendation my code now is
$data = $request->all();
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => $data['phone'],
'city' => $data['city'] ?? NULL,
'image' => $data['image'] ?? NULL
]);
return view('admin.register');
Just one other thing, anytime you find yourself using $data as a variable name - don't...
Looking at the code later it tells you absolutely nothing.
Can't argue with you. I'll write it up to be more specific. Thanks again.
Somehow
$register_form = $request->all();
User::create([
'name' => $register_form['name'],
'email' => $register_form['email'],
'password' => bcrypt($register_form['password']),
'phone' => $register_form['phone'],
'city' => $register_form['city'] ?? NULL,
'image' => $register_form['image'] ?? NULL
]);
Please sign in or create an account to participate in this conversation.