问题
After 2 years experience in cakePhp , recently i started learning laravel framework. i already created the database for my new project and create a default authentication system with laravel 5 using the
php artisan make:auth
but i cant modify it according to my database design
i have a
- users table(id,username,password,remember_token)
- user_profiles table (id,first_name,last_name etc)
- group table(id,group_name)
- group_users(group_id,users_id)
- users_user_profiles (user_id,user_profile_id)
so using the above table i want to implement a group based user management system.
if i use the default authentication system ,
- how can i store login and registration details into two tables with a single registration from? and how can i insert the id's into pivot table users_user_profiles table automatically ?
- How to create a view for the registration which contain both the login details and registration details, how can i separate them and how to access the request data separately for storing into two separate tables?
if anybody can provide a detailed description on this, it will help all laravel 5 beginners, i think its a common problem for all newbies.. please help me Thanks
回答1:
First define good your models, so you can use eloquent, for example making connection with user_profiles:
public function profiles()
{
return $this->belongsToMany('UserProfile');
}
(It will be possible only when you get UserProfile model) Then you extend your register form by adding first_name, last_name, etc. In auth controller after creating user
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
Add code that create user profile:
$userProfile = UserProfile::create([
'first_name' => $data['name'],
'last_name' => $data['email']
]);
Then add it to user by attach command (if you have many of them you can use sync) :
$user->userProfiles()->attach($userProfile->id);
More info about how user Authentication work you can find exterminating file: \vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php
来源:https://stackoverflow.com/questions/38817230/laravel-5-creating-custom-registration-and-login-from-default-authentication