Reputation: 11
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>
Upvotes: 1
Views: 1485
Reputation: 3284
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
Upvotes: 1
Reputation: 751
Try to create new user
class like that:
$user = new User;
$user->firstname = $data['firstname'];
// do same for other fields
$user->save();
Upvotes: 0
Reputation: 10714
Be sure your new properties are fillable in your User model :
protected $fillable = ['firstname', 'lastname', 'email' ...];
Upvotes: 0