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.
Related
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
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'm currently trying to figure out how to get the permissions for a role with Laravel and Entrust. It uses a pivot table called permission_role that has the role_id and permission_id stored in it.
What I'm trying to do is get the role with it's ID and then get the permissions associated with that role. I think I may be over complicating it, but since I have had zero luck on it I was hoping someone could help me out.
You may try this:
// Hope you have something like this:
class Role extends EntrustRole {}
Then get all permissions from the role whose id is 1:
$role = Role::with('perms')->find(1); // Assumed 1 is role id
dd($role->perms); // all permissions in the role
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'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!