does anyone know how to show multiple roles that are under a user? I have setup my permissions so a user can be under multiple roles, which is set easily enough using the following
$user->assignRole(['Root', 'IT', 'HR']); // assigning roles
I am however having trouble showing all roles a user relongs to in the same way?
Has anyone done this and knows how to? I simply want to show on a page which role a user belongs to.
Refer to this link
https://spatie.be/docs/laravel-permission/v4/basic-usage/basic-usage
Get the names of the user's roles
$roles = $user->getRoleNames();
Returns a collection
if there are two tables (roles and user_roles) than you can check through inner join ('inner join' to check role must exist in 'roles' table that is assigned to a user) by passing user_id (user_id to get specific roles assigned by a user).
You can get a collection of roles assigned to the user via $user->roles
Related
I am using Laravel built-in Authentication for users which I run from the terminal php artisan make:auth
It has one table in the database, users table and has an isAdmin column, so I am assuming it has two roles an admin and not_amin account.
Now, what I want is to modify this built-in functionality of Laravel make:auth to be able to add some roles like isEmployee and/or isCustomer.
How can I achieve this using the available option I have?
I hope, I somewhat explained my case. Thanks.
Having a column is_admin is targeted to only one role. This tells you if user is an admin or not.
I would suggest you to change depending on any of below cases :
1. If user can have just 1 role :
Modify users table and change is_admin to role. Inside role column, you can store the role of that particular user like admin, employee, 'customer` etc.
2. If user has multiple roles(many-to-many) :
Remove column is_admin from users table and create a new table role_user which will have user_id and role_id. You will need to have another table called roles to store all available roles in your system.
I am new to Laravel 5.3 so bear with me ;)
I am using Laratrust for permission and roles, http://laratrust.readthedocs.io/en/3.0/configuration/models/user.html
I can do:
User::with('roles')->findOrFail( $id );
and get the user with the roles, but how can I get the user with a restructured variant of roles. Instead of the array with roles I want an array with a simplified variant of Role
Thank you in advance!
I think you're trying to find out what roles a user with a specific id has.
Given id is the id of the given user try this -
User::find($id)->roles()
This will give you a list of roles the user with the given id has.
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 am having troubling linking some of my models together. Users have one of three roles:
Student
Lecturer
Admin
Information that is shared between the 3 roles is stored in a User table. When a User wants to change their role they make a role request that must be accepted by Admin.
User has one Student
User has one Lecturer
User has one Admin
User has one RoleRequest
Student belongs to User
Lecturer belongs to User
Admin belongs to User
What I am struggling with is that I cannot find a way of retrieving the data of a user without retrieving their role or their user data twice. If I retrieve the User object with recursive set to 2 I get the user data twice as it is also inside the Lecturer object. If I do the same with the Lecturer object I get the lecturer data twice as it is also inside the User object.
Don't use recursive. Set public $recursive=-1; in your AppModel, then use CakePHP's AMAZING ContainableBehavior to retrieve whatever data you'd like.
If I understood your question correctly, I'm guessing you're not using Contaible.
With that behaviour, you can do something like
$this->User->find('all', array('contain'=>'Lecturer'));
and that will retrieve an array similar to
[User] => array(/*user data*/),
[Lecturer] => array(/*Lecturer data*/)
Just remember to define your models as containable.
I have three tables in the db. One is authmanager has rolename and user_id. The second is user which has the usual id, username and password. The third table is employee, which has 'id', 'user_id' and the rest of the fields. The problem is that in the index page of employee i want to show the employees in tabs and according to the roles in the 'authmanager' table. How can the relations of yii be used?
I am using rights for managing rights and authasignments. I want to get all the users belonging specific rolename. So far i get existing roles with the help of Rights::getAssignedRoles(). I am new to rights so please give me a direction to work.