laravel relationships - 3 way pivot - eloquent - laravel

I am following a tutorial on Laravel ACL, that takes into consideration Users and Roles (https://github.com/laracasts/laravel-5-roles-and-permissions-demo). However, I have an additional level of complexity whereby I need to take into consideration a company model also. So a user may belong to many companies and each instance of a company user may have many roles.
I think the best way to acheive this would use a pivot that has 3 fields:
company_id
role_id
user_id
Esentially, I want to achive something like this:
$user->companyRoles; //return the user's company roles
$user->company->assignRole('admin');
$user->assignRole($companyId, 'admin');
Can you advise on the relationships I require to do this?
Many thanks

Related

What is “pivot table” in eloquent-relationships?

Reading Laravel 6 eloquent-relationships docs at https://laravel.com/docs/6.x/eloquent-relationships#one-to-one
I did not catch what is “pivot table” and which is practical use of it working with db in laravel ?
Thanks!
Think you have two tables like users, and roles if you want to make a relationship between them then you need to declare Many To Many relationships because the single role have many users and single users have many roles. for defining Many To Many relationships between them you need to create role_user table, here role_user table means pivot table.
for more information please read this Many To Many relationships from laravel documentation

Laravel - Pivot table with 3 models realtionships Many to Many and One to Many

I want to make authentification in my Laravel project for Users who have Roles and Permissions. Actually That's not a problem to do but Users can create Teams or be invited member of any team and each User in any Team has his ONE Role in the team(in another team the user may have another role). In essense Users must have many Roles but only one role in one particular Team and I can't understand if I create correct DB relationships.
Please check the image with relationships to get more info:
In general:
Users have many Teams, and Teams have many Users(Many to Many)
Teams have many Roles, Roles have many Teams (Many to Many)
Users have many Roles, But only one Role in a Team what relationship should be there?
I'm interested how to do that properly. How to minimize messy code in the future and avoid of need to redesign DB relationships.
Thank you guys so much!
Maybe you do need a polymorphic relationship:
https://laravel.com/docs/master/eloquent-relationships#custom-polymorphic-types
with a morph map.
https://nicolaswidart.com/blog/laravel-52-morph-map

Laravel Eloquent triple Relationship

I am stuck in situation where I need relation between 3 different tables. My tables are companies, products and Roles. Companies can assign multiple Roles to multiple products. The problem is Companies do not have any relationship with products. Products are added through admin.
Currently I have made a table company_product_role with structure company_id, product_id and role_id, the problem is how to make eloquent relation for insertion and retrieval. Either I am doing it correct or there is simple solution for it?
Any help will be appreciated.

Laravel: Table structure for multiple users types, polymorphic relationships

In my site (api using laravel 5.6 and laravel passport) I have two types of users (Teachers and Students), in the future there will be more. The teacher and student entities are very different, meaning that if I keep them all in one table, the table will be long and many fields will have a null value. Right now I have one Users table with common fields and two other tables (Teachers and Students) to which I have setup a polymorphic relationship from user. My question is if this is a good approach, or if there are other ways to handle this more elegantly?
I would create 1 table for Teachers and 1 table for Students and not use the Users table/model. This way you can keep them completely separate and not worry about adding more types of users in the future. Continually trying to fit new users into your existing Users model, which would be shared, is a headache. I made this same mistake when I started and eventually had to rework the project.
There are plenty of guides for Laravel multi-auth / multi-user online.
Here are a couple to help you get started:
https://medium.com/hello-laravel/multiple-authentication-system-laravel-5-4-ac94c759638a
https://www.codementor.io/okoroaforchukwuemeka/9-tips-to-set-up-multiple-authentication-in-laravel-ak3gtwjvt
Also, there are cases where it makes sense to use the User model for multiple types of users. For example, you may have multiple roles for a user where most/all of the fields are the same (not your scenario). In this case, you can assign a 'role' to each User and the check the roles for actions (e.g. add middleware to prevent roles from accessing various routes). Here is an example:
https://medium.com/#ezp127/laravel-5-4-native-user-authentication-role-authorization-3dbae4049c8a
Since you said the teacher and student entities are very different, you should keep them separate.

Complicated Eloquent Relationships

I have a many-to-many relationship, users and roles, which I have worked out just fine. However, I have another table I would like to relate as well, called leads. A user and that user's role can be assigned to a lead.
So for example, I've created a Role and let's call it 'Manager'. Now, when I am managing my Users, I'd need to be able to assign different users to different roles which will be a many-to-many relationship (a role can have many users, a user can have many roles). So I will assign the role of 'Manager' to User A.
Now, when I am modifying my leads, I'd like to be able to assign a role_user to my lead (In my example, I'd like to assign a user to that lead), but I'd first need to assign a role to that Lead (Manager), and then be able to assign a user that is of that role to that lead.
Currently, I've got a many-to-many relationship setup for users and roles, using the pivot table name role_user. I've then got a many-to-many relationship setup on that pivot table, role_user, and leads, using another pivot table named lead_role_user.
Models + Controller - http://paste.laravel.com/D6h
My error: Call to undefined method Illuminate\Database\Query\Builder::roles()
It feels as though I am making this much more difficult than it should be.

Resources