簡體   English   中英

如何在Laravel 5中熱切加載多個嵌套子關系?

[英]How to Eager-load multiple nested sub-relations in Laravel 5?

我有一個故障的復雜數據結構:

Event: top-level object
|-Streams: events can contain multiple streams of information / data capture
  |-Datacapture: each stream can have up to 4 datacapture records
  |-Overlay: each stream can have many overlays
  | |-Frames: each overlay can have many frames
  |-Background: each stream can have many backgrounds
  | |-Frames: each background can have many frames
  |-Experiences: each stream can have up to 6 experiences
    |-Selectors: each experience can have many selectors
      |-Filters: each selector can have one filter
      |-Props: each selector can have one prop (whether it has a filter or no)
      | |-Frames: each prop can have many frames
      |-Overlay: each selector can have one overlay
      | |-Frames: each overlay can have many frames
      |-Background: each selector can have one background
      | |-Frames: each background can have many frames

這只是難題的一部分-體驗和信息流附加了更多的對象,但是我認為這很重要。

理想情況下,我希望做的是創建一個渴望加載查詢,該查詢可以加載並返回包含所有組成部分的完整EVENT集合。

我知道我可以渴望與with(['blah','plop'])加載多個關系,並且我可以與with('blah.plop')加載嵌套的關系,甚至可以通過with(['blah.plop','blah.somethingelse']) ,但是我的情況顯然更加復雜。

我想避免像這樣分別加載每個“子關系”

$event = event::with([
    'streams.datacaptures',
    'streams.overlay.frames',
    'streams.background.frames',
    'streams.experiences.selectors.filters',
    'streams.experiences.selectors.props.frames',
    'streams.experiences.selectors.overlay.frames',
    'streams.experiences.selectors.background.frames'
    ])->find($eventcode);

我能做些更整潔的東西嗎?

好吧,我不知道這對您來說是否更整潔/更清潔。

渴望與模型事件建立負載關系

protected $with = ['streams'];

同樣去其他..與流

protected $with = ['datacaptures','overlay','background','experiences'];

在背景和疊加中

protected $with = ['frames'];

等等 ..

默認情況下,這將調用模型具有的所有關系..但缺點是即使您不需要它也將渴望加載。

所以你只需要

$event = event::find($eventcode);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM