Laravel为关系添加/寻找关系

时间:2013-01-20 17:22:10

标签: orm laravel relationships eloquent

如何找到与Eloquent ORM关系的关系?目前我有这样的事情。简单的关系。我可以找到Image和它的摄影师。现在我需要做一些更复杂的事情,我还需要找到摄影师的标签。

转储看起来像这样

object(Image) {
    ["attributes"] => [],
    ["relationships"] =>
        ["photographer"] =>
            ["attributes"] => [],
            ["relationships"] =>
}

但我需要添加标签关系,所以看起来像这样

object(Image) {
    ["attributes"] => [],
    ["relationships"] =>
        ["photographer"] =>
            ["attributes"] => [],
            ["relationships"] =>
                ["tags"] =>
                    ["attributes"] => [],
                    ["relationships"] =>
}

这怎么可能?

/图片模型

public function photographer()
{
    return $this->belongs_to('Photographer');
}

public function tags()
{
    return $this->has_many_and_belongs_to('Tag', 'tag_relationships');
}

/控制器

$images = Image::with(['photographer'])->order_by('updated_at', 'desc')->get();

1 个答案:

答案 0 :(得分:3)

你只需使用laravel的点语法:

Image::with(['photographer', 'photographer.tags', 'photographer.tags.categories]) ....