Reputation: 1618
I am trying to create a laravel application with version 5.2. Where there will be 3 types of users
users
" table for
this. owners
" for this.customers
" for this.Now my problem is:
i want to make sure login Owners will get proper authentication and redirect to their own (other then default Auth route) route.
And same with customer, and they will be mainly login through frontend of the website, so their route will be different from owners and Administrator. And these customer will also get authentication.
How can i manage that? I have worked around with single table, but being as a new person to Laravel i am not sure how i can achieve with multiple table.
I have checked laravel 5.2 started supporting multiple gaurds now, but not sure how can i do this.
There are certain packages for this, but i dont want to relay on package for this.
Thank you!
Upvotes: 0
Views: 581
Reputation: 3572
I would suggest you follow a Polymorphic approach for this.
Let's say there are three different tables - administrators
, owners
, customers
Now for all of them, there is a common table with the name users
which will have the columns :- profile_id, profile_type.
Now profile_id
will become the foreign key for tables administrators
, owners
and customers
and profile_type will tell which Model the user belongs to.
Relation would be like,
class User {
public function profile() {
return $this->morphTo();
}
}
--
class Administrator {
public function user() {
return $this->morphOne('App\User', 'profile');
}
}
Here we are using morphOne
instead of morphMany
because the profile_id field in users
table should have only one row for one admin.
Lastly, for the purpose of creation/storing. You'll have to :-
Create an admin like
$admin = Administrator::create($inputs);
Then do
$user = new User($inputs);
$admin->user()->save($user);
You're done!
You can learn more about it this approach from https://laravel.com/docs/5.1/eloquent-relationships#polymorphic-relations
Thanks,
Upvotes: 1