Retrieving all morphedByMany relations in Laravel Eloquent

我的梦境 提交于 2020-06-24 11:08:54

问题


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?


回答1:


I use a trick here:

Create a Model Taggable for your connection table taggable and add a hasMany relation to the Tag model.

public function related()
{
    return $this->hasMany(Taggable::class);
}

Within your Taggable model create a morphedTo relation.

public function taggables()
{
    return $this->morphTo();
}

Now you can get all models witch are using the tag by calling:

$tagged = Tag::with('related.taggables');



回答2:


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?

class Tag extends Model
{
    [...]

    /**
     * Get all of.
     */
    public function morphed()
    {
        return $this->video->union($this->posts)->all();
    }
}



回答3:


You should be able to add a relationship on your Tag class as such

public function taggable()
{
    return $this->morphedTo();
}

That will use the taggable_id and taggable_type to get the relevant Model that it is tied to.



来源:https://stackoverflow.com/questions/35948130/retrieving-all-morphedbymany-relations-in-laravel-eloquent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!