I'm trying to build a Laravel app using a customized database structure. I have tables types
, units
, content
, and a pivot table called relations
. The structure of the relations
table is such:
---------------------------------------------
| id | relation_type | first_id | second_id |
---------------------------------------------
| 1 | type_unit | 1 | 2 |
---------------------------------------------
| 2 | unit_content | 2 | 3 |
---------------------------------------------
In other words, the first three tables have many-to-many relations among themselves, and the fourth one is the pivot table for all the relations. How can I use the Eloquent's BelongsToMany
method with this pivot table structure, i.e. how can I select only the pivot table's records relevant to the given relation? For example, how would I use only the type_unit relations in this:
class Type extends Eloquent {
public function units()
{
return $this->belongsToMany('Unit', 'relations');
}
}
but at the same time ignore the unit_content relations?