Laravel - How should I create two separate register pages? - laravel

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.

Related

Creation of related Models on auth RegisterController in laravel

In my application, I have tables related to users and some of these tables require a row be inserted as soon as a User is registered.
For example, I have the following models:
UserProfile,
UserAssignedRole,
UserSettings
As soon as a User successfully registers, they will need a corresponding row in the DB.
I've done the following:
protected function create(array $data)
{
$user = User::create([
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
UserProfile::create([
'user_id' => $user->id,
]);
UserAssignedRole::create([
'user_id' => $user->id,
'role_id' => 1
]);
UserSettings::create([
'user_id' => $user->id,
'nsfw' => 1
]);
return $user;
}
I'm looking for a better and more elegant way to achieve this. Is there a "Laravel" way in achieving this?
In the Larvel docs there's a chapter about Recursively Saving Models & Relationships. This is probably what you're looking for. They specify the following example:
$post = App\Post::find(1);
$post->comments[0]->message = 'Message';
$post->comments[0]->author->name = 'Author Name';
$post->push();
You could achieve something similar by creating a user including all its relationships in one push().

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 User - Role many to many relationship

I have created a Role table which has a many to many relationship with the User table. Models and everything in place plus i can seed all tables correct to create a default user + 3 roles.
I want to modify the scaffolded register controller so i can attach a role along with the other attributes (name, mail etc). The default create method looks something like this:
protected function create(array $data)
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
how do i call Role model ? and how do i pass it in the create function and ultimately to the view ?
My initial thought was:
use App\Role;
at the beginning of the file and
$roles = Role::all();
maybe on the create function ? that is probably wrong but i cannot think something else.
Plus how do i call it after on the view ? Role table has 3 predefined roles as i mentioned above.
You need to create user first, then you need to attach roles and return user instance. This should work for you:
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$roles = [2, 4];
$user->roles()->attach($roles);
return $user;
}

Laravel 5.3 - Register Controller

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'])
]);

How do I modify my Laravel 5.2 registration to assign new users a role in Laravel 5.3?

I'm new to Laravel, and have been trying to build a user-role authentication system. As 5,3 is very new, there are not many resources that are beginner-friendly. I am using the authentication system created by php artisan make:auth, however I am unsure on how to associate my newly created users with a my roles table.
Initially I had been following this video series on YouTube, but the Authentication controllers differ greatly between 5.2/5.3
I'm hoping that someone can help me figure out what must be done to add in an equivalent what is done in the video to the new controller.
Laravel 5.2 Role Association
public function postSignUp(Request $request)
{
$user = new User();
. . .
$user->save();
$user->roles()->attach(Role::where('name', 'User')->first();
}
From Laravel 5.3 RegisterController.php
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
Thanks for reading.
You can modify the auto-generated Controllers:
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$user->roles()->attach(Role::where('name', 'User')->first());
return $user;
}
To attach a role to a user by inserting a record in the intermediate table that joins the models right after creating an account, use the attach method before returning the new User, like this:
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
$user = User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
//attach default user role to user
$user->roles()->attach(Role::where('name', 'User')->first());
return $user;
}

Resources