1

I want to register new users in my laravel application using the provided inbuilt functionality but also add two more fields: firstname and lastname. I am doing my registration in the admin area of the application and so i have a customized form that sends the data to the registration route. When I have an empty users table, I am able to add a new user to the database. When I use dd command I see the data sent from the client too. But when I try to add second user to the database, nothing happens. There are no errors displayed whatsoever. This time I try to use dd command and the application does not return anything.

To emphasize I do not wish to create another controller to handle this, but rather expand on the RegisterController that is already provided.

I need help to identify what I am doing wrong.

// In User model

 protected $fillable = [
        'firstname', 'lastname', 'email', 'password',
    ];

// get data from client in RegisterController.php

protected function validator(array $data)
    {
        return Validator::make($data, [
            'firstname' => ['required', 'string', 'max:255'],
            'lastname' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

// save new user in RegisterController.php

 protected function create(array $data)
    {
        dd($data);
        return User::create([
            'firstname' => $data['firstname'],
            'lastname' => $data['lastname'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }

// My form

<form  method="POST" action="{{ route('register') }}">
    @csrf
    // code suppressed

 </form>
Rufusy
  • 11
  • 1
  • 4
  • Please show us your _routes_ and also the code changes that allow you to access `$data` directly. Normally, you'd get that data from the `$request` object. – waterloomatt Jul 10 '19 at 12:39
  • Might as well show the output of `dd($data)` too. – waterloomatt Jul 10 '19 at 12:48
  • As Waterloomatt says you should use the request object either in the method signature or the in the body with the helper ```request()->input('firstname')``` – Ulrik McArdle Jul 10 '19 at 20:00

3 Answers3

1

I am assuming you've separated admin and other users into two different groups.

TIP : You cannot use Auth::routes(); two times so it's better if you write all those default routes manually to get more clear picture, something like this

Route::group([
    'as'        => 'admin.',
    'prefix'    => 'admin',
    //'namespace' => 'Admin',
    //'middleware'  => 'SomeUserMiddleware'
],function(){
    Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
    Route::post('register', 'Auth\RegisterController@register');
});


Route::group([
    'as'        => 'user.',
    //'prefix'    => 'user'
    //'namespace' => 'User',
    //'middleware'  => 'SomeUserMiddleware'
],function(){
    Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
    Route::post('register', 'Auth\RegisterController@register');
});

Here you can find list of all other default routes https://stackoverflow.com/a/52192841/5928015

Go to following path to check what all methods we can override :-

...\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php

and to update redirect path

...\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RedirectsUsers.php

Vipertecpro
  • 3,056
  • 2
  • 24
  • 43
0

Be sure your new properties are fillable in your User model :

protected $fillable = ['firstname', 'lastname', 'email' ...];
Vincent Decaux
  • 9,857
  • 6
  • 56
  • 84
0

Try to create new user class like that:

$user = new User;
$user->firstname = $data['firstname'];
// do same for other fields
$user->save();
Guga Nemsitsveridze
  • 721
  • 2
  • 7
  • 27