Laravel 5.3 - Register Controller - laravel

I have a question.. i create a new laravel project and do the auth::make. The login and the register worked and save the data from register in my local data base. But now i have a problem. I have a custom controller (for example "myController.php) and inside this controller i have a function called "register". In my function i receive the Request $request and get the data (name, email and password) (I test and it worked, i receive the correct data in request). Now how is it possible to save this data in local database using the Laravel Auth but using at the same time my controller and my function?
Thanks ;)
Regards

You can just create a new user manually in your own controller like Laravel does:
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password'])
]);

Try putting this into your register function:
User::create([
'name' => $request->input('name'),
'email' => $request->input('email'),
'password' => bcrypt($data['password'])
]);

Related

Save extra data while User Registration

I am using Breeze template.I am working with User Registration. My store() user function is like below
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|confirmed|min:8',
]);
Auth::login($user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]));
event(new Registered($user));
return redirect(RouteServiceProvider::HOME);
}
I would like to save more data (like user Date of Birth) as User details in another table while Registering User. I would like to maintain one to one relationship with User table and User details table.
Actually I would like to attache something with this event(new Registered($user));. So that I can save data to another Table along with registration. How can I do that ?
Where can I get the code of this new Registered($user) ?
create a new instance
and save all other fields with id to another table
$model2 new model2();
$model->col = $request->1;
$model->fk= $user->id;
$model->save();

Laravel: send email on registration

I want to be able to send emails through the registration of my system. I have set up all the mailtrap information and included all my key imports. However im not sure if the actual way of sending the email is correct. I keep getting error: undefined variable email, see the code below:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
Mail::to($data['email'])->send(new WelcomeMail());
}
}
I have imported welcomemail and the facades import so everything is good to go, however i am unable to send the email as of now.
Any code after return will not execute. Do all your logic and then return.
$user = User::create([...])
Mail::to($user)->send(new WelcomeMail());
return $user;
I also did:
Mail::to($data['email'])->send(new WelcomeMail());
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
and this worked perfectly fine

Laravel 5 not generating api token on register

I have added a column for api_token and in my register controller , while creating the user I am trying to generate a unique id , but its not generating any code . Here is my create user function in register controller
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'api_token' => md5($data['email'].$data['name']),
'password' => bcrypt($data['password']),
]);
}
Do I need to add this somewhere else?
As cbaconnier mentioned, you also need to add api_token to your $fillable array in the User model.

Laravel - How should I create two separate register pages?

What would be the best approach for me to create two registration pages in Laravel 5.2? One should assign the user's role to 'buyer' and the other one to 'seller'.
Thanks in advance!
Just create two views and pass some variable in registration form, like:
{!! Form::hidden('role', 'user') !!}
And then change create() method in RegisterController (if you're using 5.3) or AuthController (for 5.2) to something like this:
protected function create(array $data)
{
return User::create([
'role' => $data['role'], // This will set role from hidden form element
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
Don't forget to add role to $fillable array in User model.

How to insert form values in two different database table in laravel 5.1

How should I save values of form in two different tables in database using laravel.
I have two tables one is saving only email and password, second is storing other information of user.
How should I do this?
Have the form send to a particular Controller method and Store the pertinent data in their relative Models:
public function store(Request $request)
{
User::create([
'email' => $request->email,
'password' => Hash::make($request->password)
]);
Profile::create([
'website' => $request->website,
'address' => $request->address,
// etc...
]);
}

Resources