0

I have a faily complex data structure:

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

This is just a piece of the puzzle -- there are more objects attached to experiences and streams, but I think this gives the gist.

What I wish to do, ideally, is create a single eager-load query that can load and return the complete EVENT collection with all of it's constituent parts.

I know I can eager load multiple relationships with with(['blah','plop']) and that I can load nested relationships with with('blah.plop') and I can even load multiple nested relationships with with(['blah.plop','blah.somethingelse']), however my situation is obviously more complex.

I want to AVOID loading each "sub-relationship" separately like

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

Is there something neater / cleaner I can do?

1
  • Turns out, at least with Laravel 5, as far as I can tell, that these get intelligently combined and elements are not repeated. Commented Oct 9, 2017 at 15:15

1 Answer 1

1

well i don't know if this would be neater / cleaner for you ..

eager load relations with your model event

protected $with = ['streams'];

same goes to the other .. with stream

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

in background and overlay

protected $with = ['frames'];

and etc ..

by default this would call all the relationship the model has .. but the con is it will eager load even you doesn't need it ..

and so you will just have to

$event = event::find($eventcode);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.