Laravel Eloquent triple Relationship - laravel-5

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.

Related

Database and code design for category and sub category in laravel

I have users in my laravel site, each user has several posts, each posts has several category, each category has several subcategories. How should be database and php structure. Currently I have users table with id, name....and posts table with id, user_id .....I have some belongstomany function. But I don't know this much depth/inner go, like users>posts>category>subcategory. Please give outline. Thanks.

How to link two different tables in one relational table in laravel

I have two different tables client and admin, i want to add relation in one table post has many relation. Overall scenario is client and admin can multiple posts. How can do it in laravel?

laravel define relation over multiple tables

I have a table customers with the fields id, name and so on.
One table doctors with the fields id, name.
Then there is one table subject_areas which has all subject areas which a doctor can have. The fields are id, text.
So, each doctor can have multiple subject areas. There is one pivot table doctor_subject which is a belongsToMany relation.
Here is my problem: A customer can have multiple doctors, but only for a specific subject area. I tried it with a new table customer_doctor with the fields id, customer_id and doctor_subject_id. But how do i map this in Eloquent?
Issue was in relation between tables. After chat clarification this came out as solution:
Html form is written in a way that customer first choose doctor, then depending on selection choose several of his available areas.
In that scenario customer needn't to be related to areas directly and should be related to areas only over relation with doctor.
Also as side note, if needed deeper relations, models on pivot tables could be created and used as well.

laravel relationships - 3 way pivot - eloquent

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

laravel 4 polymorphic relationships

I'm trying to get my head around using polymorphic relationships in Laravel 4, but I can't work out how to do the following:
I have a users table, customers, suppliers and partners tables and a userhistories table which has a one to many with users and morph many (called historable) with customers, suppliers and partners.
I want to get the latest 10 userhistories for the (Auth::user) logged in user that are linked to the customers table without including those for the other two tables, but I can't work out the Fluent query(s) to let me do this. Help! Thanks.

Resources