Laravel 4.1: proper way to retrieve all morphedBy relations?

后端 未结 2 680
感情败类
感情败类 2021-02-09 05:40

Just migrated to 4.1 to take advantage of this powerful feature. everything seems to work correctly when retrieving individual \'morphedByXxxx\' relations, however when trying t

2条回答
  •  滥情空心
    2021-02-09 05:48

    Was able to figure it out, would love to hear comments on this implementation.

    in Tag.php

    public function taggable()
    {
        return $this->morphToMany('Tag', 'taggable', 'taggables', 'tag_id')->orWhereRaw('taggables.taggable_type IS NOT NULL');
    }
    

    in calling code:

    $allItemsHavingThisTag = $tag->taggable()
                    ->with('videos')
                    ->with('posts')
                    ->get();
    

提交回复
热议问题