0

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 ,

  1. 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 ?
  2. 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

2
  • Are you aware of the Eloquent ORM? Commented Aug 7, 2016 at 18:52
  • yes, i know about relationshiops Commented Aug 8, 2016 at 5:38

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

What about updating pivot tables? I have a group_user pivot table as mentioned , how to update it after saving the user details automatically
Same as saving to users_user_profiles by $user->userProfiles()->attach($userProfile->id);
what i want is , when a user is registering using registration page, i am passing a hidden field as group_id . i want to automatically update my pivot table group_user when the user data saved.?
first find that group using $group = Group::first($request('group_id'); then add it to user $user->groups()->attach($group); consider that passing group_id in hidden field can be big secure issue
is there any way to pass the group id? i have two registration form, one for user and one for employee and a common login page.how can we identify them on registration , other than passing hidden field
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.