简体   繁体   English

Laravel为关系添加/寻找关系

[英]Laravel adding/finding relationships for relationships

How to find relationships for relationships with Eloquent ORM? 如何找到与Eloquent ORM关系的关系? Currently I have something like this. 目前我有这样的事情。 Simple relationship. 简单的关系。 I can find Image and it's photographer. 我可以找到Image和它的摄影师。 Now I need to do something more complex, I need to find also photographers tags. 现在我需要做一些更复杂的事情,我需要找到摄影师的标签。

dump looks like this dump看起来像这样

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

But I need to add tags relationship so It would look like this 但我需要添加标签关系,所以它看起来像这样

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

How is that possible? 怎么可能?

/Image model /图像模型

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

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

/Controller /控制器

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

你只需使用laravel的点语法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM