Retrieving all morphedByMany relations in Laravel Eloquent

后端 未结 3 1180
半阙折子戏
半阙折子戏 2020-12-31 01:01

In the Laravel documentation, there is the following example for retrieving morphedByMany relations, which are many-to-many polymorphic relations.

Larav

3条回答
  •  一生所求
    2020-12-31 01:54

    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();
        }
    }
    

提交回复
热议问题