Laravel 4.1: proper way to retrieve all morphedBy relations?

后端 未结 2 845
野的像风
野的像风 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:57

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

提交回复
热议问题