Laravel 5 not generating api token on register - laravel-5

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.

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();

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 - 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 add data to related table in Auth Laravel?

I use the standard mechanism authentication in Laravel 5.3 is built by command:php artisan make:auth.
So, It have created controller RegisterController with the following methods:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]));
}
So, I need to extend this method that to add data to related table for User table. For this I do:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
])->roles()->attach(Role::where('name', "admin"))->first());
}
After this I get error:
FatalThrowableError in SessionGuard.php line 441:
Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in \vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php on line 32
I didn't use Laravel's auth logic, but it looks like the user gets logged in right after they're created. To log the user in Laravel needs the User object, hence:
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
The create() method returns Model. You, however, added attach() to the return statement, and if you take a look at the API, the method returns void which eventually translates to null mentioned in the error message.
What you want to do, is something like this:
protected function create(array $data) {
// Create the User and store the object
$newUser = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
// Add extra data
$adminRole = Role::where('name', 'like', 'admin')->first();
$newUser->roles()->attach($adminRole->id);
// Return the new User object
return $newUser;
}
Note: I assumed you have an id column in your role(s) table.

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