Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SerhiiNuzhnyi's avatar

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');
} 
0 likes
7 replies
Snapey's avatar
Snapey
Best Answer
Level 122

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,

1 like
Snapey's avatar

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

1 like
michaelrtm's avatar

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
}
1 like
nanpaul68's avatar

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');
}
1 like
SerhiiNuzhnyi's avatar

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');
Snapey's avatar

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.

SerhiiNuzhnyi's avatar

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 or to participate in this conversation.