laravel define relation over multiple tables

Viewed 98

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?

1 Answers

Issue was in relation between tables. After chat clarification this came out as solution:

enter image description here

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.

Related