Multiple hasMany or belongsToMany relationships

72 Views Asked by At

I have two models Category and Object and relationship table object_has_categories.

Category:

public function objects() {

        return $this->belongsToMany(Object::class,Object::TABLE_CATEGORIES, Object::COL_CATID, Object::COL_OBJID );
}

Now, I want to split the Object table and relationship table into multiple tables. The tables have a predefined prefix (<type>_objects and <type>_object_has_categories). I use the original class as a base (abstract class) and override TABLE property in the derived classes with the specific table name.

The number of Object tables (types) will be deployment-dependent.

How can I write a method to return all objects for a given category?

One way I just came up with is since I have a method to return all enabled object types, I can imagine a cycle to iterate over all $this->belongsToMany(..) relationships. But how to combine the results of multiple belongsToMany calls?

0

There are 0 best solutions below