【问题标题】:Laravel adding/finding relationships for relationshipsLaravel 为关系添加/查找关系
【发布时间】:2013-01-03 19:50:43
【问题描述】:

如何使用 Eloquent ORM 查找关系?目前我有这样的东西。简单的关系。我可以找到图片,它是摄影师。现在我需要做一些更复杂的事情,我还需要找到摄影师标签。

转储看起来像这样

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

【问题讨论】:

  • 在 IRC 获得了答案。也许这会起作用 Image::with(['photographer', 'photographer.tags']),明天会测试它。

标签: orm laravel relationships eloquent


【解决方案1】:

你只需使用 laravel 的点语法:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-11-25
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
相关资源
最近更新 更多