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.
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?
There is a project of Hotels, and for each hotel there is one admin.
What I have done:
In Usertype table different types of Users like chef,managers,hotel
If user type is hotel means it is the admin of that hotel.
Now for each and every CRUD and other functionalities I get hotel id like this
$hotel_id = \Auth::user()->hotel_id;
and checked this hotel_id in each query(like get user, save user, get invoice,set invoice,check food etc).
Is this the correct way?
Yes, it's a correct way to check if a user is hotel's admin. But it's better to use policies for that:
public function update(User $user, Hotel $hotel)
{
return $user->id === $hotel->user_id;
}
Policies are classes that organize authorization logic around a particular model or resource.
https://laravel.com/docs/5.5/authorization#generating-policies
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,
I want to have an authentication for Admin and for Non-admin. I'm having the admin infromation in 'users' db table, and non-admin information in 'customers' table.
How to use Laravel's Auth class to create authentication for this two different user roles?
for starter you should merge the two tables and then define a new column named "role_id" and then a new table called "roles" then as you attempt like this:
if (Auth::attempt(array('email' => $email, 'password' => $password)))
{
Session::put('role', Auth::user()->role_id);
return Redirect::intended('dashboard');
}
then via Session you can get the role whenever it's needed and decide to whatever yo want to do with it...
note: also edit your User model so role_id can be accessible
happy coding
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!