I am trying to create a laravel application with version 5.2. Where there will be 3 types of users
Administrator (website manager) - using default "users" table for
this.
Owners (Website listing creator from frontend) - using a
table "owners" for this.
Customer (Visitors or registered
visitors) - using a table "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!
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,
Related
I'm relatively new to PHP and just started to learn Laravel. I leave a few questions regarding a personal learning project I'm working on. Thank you in advance for any feedback!
CONTEXT:
My project is using the default laravel 7.x UI and auth scaffolding. I'v implemented user roles with a roles table and a role_user table. This works as intended (e.g. I can perform CRUD on users and their roles)
Many users can have many roles.
ROLES:
Customer
Agent
Admin
I run into trouble when trying to implement user Statuses. Like this:
Users with the Role of Agent can have only one Status at any given time; either 'AVAILABLE' or 'BUSY'.
QUESTIONS:
#1. How should I define the Agent User/Status relationship? Initially I did Many to Many. I created a statuses table and status_user table. Later I concluded that this was incorrect and changed the relationship to Many to One as seen below. ( I didn't change any of my tables)
In the Users Model:
public function statuses(){
return $this->belongsTo('App\Status');
}
In the Status Model:
public function users(){
return $this->belongsToMany('App\User');
}
Some thoughts:
After having trouble with some queries I'm starting to think using the two tables (statuses and status_user) is in incorrect given how I modeled them above.
Should I rollback and just add a "status_id" column to the user table while keeping the tables as is? I'm starting to think I'm getting the entire relationship wrong.
Maybe I shouldn't be relating status to the users table; instead to a separate 'Agents' table?
I am new to Laravel and i am creating a new SMS (Student Management System) for my first Laravel Project. So, i need 3 tables.
Admins
Teachers
Students
Admins will be me, teachers will be the actual teachers and students will be students. I will have more tables in the futures but i like to keep all the different types of users in their own table. This was simple before laravel. I am in 5.6 and i am using this to login a user:
public function auth()
{
if (Auth::attempt(['username' => request(['username']), 'password' => request(['password'])])) {
return redirect('/admin');
}
}
I want to know how i can get the Auth::attempt to check the "admins" table when trying to auth someone in the admins controller. And how do i do this so that way when i am working on the teacher/student authentication, it will check their specific table. I am so confused.
I have to create an application in which there are 5 types of accounts
Super admin
Inventory admin
Shop owner
Shop manager
User
So How can I create these multiple accounts with Laravel, should I use different middleware group for each. also, i have to create privileges with all.
Currently, I am using it with different middleware and session for each and separate tables in DB for each, but I don't think so that's a good way to do it.
What is the way to create these multiple accounts with Laravel.
#Thanks
In addition to providing authentication services, Laravel also provides a simple way to authorize user actions against a given resource. Laravel's approach to authorization is simple, and there are two primary ways of authorizing actions: gates and policies. Please refer the Laravel documentation for more details.
Authorization in Laravel 5.5
As #Fawzan has said, if you only working with Roles (or a Group) then just create a groups table and then link each user to the appropriate group. You then have a single user table with a groupID for each user.
Then you could create a blade directive to help you in your app to check if a user has a specific role. (Place this inside your AppServiceProvider)
Blade::directive('hasRole', function ($role) {
return auth()->user()->role->name = $role;
});
// Or a little more performant if you call the directive many times.
$roles = Role::all()->pluck('id', 'name')->toArray();
Blade::directive('hasRole', function ($role) use ($roles) {
return auth()->user()->roleID == $roles[$role];
});
You can add a column to users table called user_type and
insert the decimal value for each user for example
Super admin = 1;
Inventory admin = 2;
Shop owner = 3;
Shop manager = 4;
User = 5;
and based on the user_type you can do all your operation.
I was working on making a group functionality for my website which uses a many to many relationship between groups and users.
My User model looks like this:
public function groups(){
return $this->belongsToMany('App\Group')->withPivot('role')->withTimestamps();
}
My Groups model looks like this:
public function users(){
return $this->belongsToMany('App\User')->withPivot('role')->withTimestamps();
}
So my third column has the name of role which is a string variable and is set to a default of "member" for members of my group and I set it to "admin" for the actual user who creates a new group. But I want the admin to have the option of making multiple members admins as well which would require me to check weather the current current user who sent the request is an admin or not. If he is, then I wanna be able to take his request of making a member an admin which would require me to update the role for that particular "member" to an "admin".
In the laravel documentation it only shows you how to attach and detach data in a pivot table and else where I have only seen methods of retrieving data from the first two columns but how can I do the same for additional columns and also be able to update it using the updateExistingPivot method?
You could access the column simply using pivot e.g :
$user->pivot->role
Take a look at Retrieving Intermediate Table Columns in documentation Eloquent Relationships.
Hope this helps.
I'm using Codeigniter with Ion Auth for user management. I'd like to create two different user roles - one for volunteers and one for organizations. I know that I can use Ion Auth's groups for things like access control, but what would be a good way to add fields to the different user types (for instance - the volunteer user will have a 'languages spoken' field while the organization will have a 'mission' field)? Should I extend the Ion Auth class for each new user type and handle CRUD seperately for each type, or use the 'groups' field and the user id to reference the fields in another table? Any insight as to an approach to this common problem?
I would recommend just adding all the fields you need into the meta table and only updating the ones you need per user group. If you need multiple groups per user check out Ion Auth v2.
I had the same problem before, what I ended up doing was building relation tables to handle different groups with different fields. Then I modified the profile() code a bit, to join the additional relation table according to the user's group settings.
I would start off building a relational database.
example:
**volunteers table**
id
user_id
languages
**organizations table**
id
user_id
mission
Then depending on user group, join the table in profile() function.
Hope this helps!