Is it recommended to represent many-to-many relationships as individual models in Eloquent?

  activerecord, eloquent, laravel, orm

I am looking at my current project that I took over, and I notice that all the models written as PHP classes that extend Eloquent’s Model class are atomic entities, or, polymorphic entities (like there’s a user model, and then there’s special types of users).

I was wondering if it is common to have models (individual class files) for entities that are composed of the many-to-many entities, especially if they have properties of their own that can be changed and accessed.

I notice that I can use eloquent’s fluent syntax for atomic entities, but for many to many entities that describe relationships, in order to query/update/insert into the database I have to use the DB::insert…, etc as a facade to get attributes from this specific pivot table.

Based on my limited experience in Laravel so far, I’m thinking it is possible (with belongsToMany in the atomic entities, and hasOne for each of the endpoints in the relationship), but I don’t know if it is good Laravel practice.

Source: Laravel

Leave a Reply