dimanche 26 mai 2019

Create relationships through ids table in Laravel

I am new to Laravel and I have some queries to Relationships.

I was through the documentation of Laravel but couldn't find a clear solution.

So I have 3 tables.

-- Table Users (id, name, ....)
-- Table Departments (id, name....)
-- Table C (id, user_id, department_id)

So I want to create a relation that User has many Department and each Department has many Users. I want link them through table C with their ids.

So propably this should be with hasManyThrough method. What i can not clear in my head is which one should be hasMany and which belongTo ?

maybe i am trying to link them the wrong way. If there is any cleaner way to link them as manyToMany please let me know.

Also when I am creating a new User how can i make it auto insert the ids in the database with Eloquent Model?

Thnak you in advance.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2EA0Vm5
via IFTTT

Aucun commentaire:

Enregistrer un commentaire