0
votes

My relationships is something like:

player
   sub
       something
   sub-2
   sub-3
       something
           foobar
   sub-4

I can get player by id with its sub like this:

Player::with('sub.something','sub-2','sub-3.something.foobar','sub-4')->find($id);

However I had to specify each 'with' relationship manually. Is it possible to just get everything on the child relationship (hasMany/hasOne) in one query? Something like:

Player::with(everything())->find($id);
3
if you define what 'everything' is ... the model does not know what methods are relationship methods ... so sure if you wanted to create some method/macro that adds all those withs to a builder you could easily do itlagbox

3 Answers

1
votes

The short answer is no, relationships are expensive to get so you need to be explicit with what you need to load each time. You can however specify which relationships are always eager loaded on the model:

class Player extends Model {
    protected $with = [ 'sub.something','sub-2','sub-3.something.foobar','sub-4' ];
    // Rest of model
}

This way you won't need to specify ::with every time when loading a model.

0
votes

Yes You can use Eager Loading to get the model with related data: eager-loading

0
votes

Try this:

Player::with(['sub'])->find($id);

it will return all subs of a player.