How to link two different tables in one relational table in laravel - 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?

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 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.

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 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