In the Laravel documentation, there is the following example for retrieving morphedByMany
relations, which are many-to-many polymorphic relations.
Laravel Many to Many polymorphic relations documentation
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
/**
* Get all of the posts that are assigned this tag.
*/
public function posts()
{
return $this->morphedByMany('App\Post', 'taggable');
}
/**
* Get all of the videos that are assigned this tag.
*/
public function videos()
{
return $this->morphedByMany('App\Video', 'taggable');
}
}
How would I get a list of all morphed
relations in one query / collection, for instance, posts
and videos
, and then if I later added photos
(or anything), that too?
You should be able to add a relationship on your Tag class as such
That will use the taggable_id and taggable_type to get the relevant Model that it is tied to.
I use a trick here:
Create a Model
Taggable
for your connection tabletaggable
and add ahasMany
relation to theTag
model.Within your
Taggable
model create amorphedTo
relation.Now you can get all models witch are using the tag by calling:
Did you think to use the "union" function of the collections to merge all the different collection in order to retrieve all what you need?