Knowing the structure below, can I add a function to the Container
class to get records
directly with Container::with('records')->get()
instead of Container::with('boxes.letter.records')->get()
?
containers
hasMany
boxeshasOne
letterhasMany
records
class Container extends Model
{
public function boxes(): hasMany
{
return $this->hasMany(Box::class);
}
public function letters(): hasManyThrough
{
return $this->hasManyThrough(Letter::class, Box::class);
}
}
class Box extends Model
{
public function letter(): hasOne
{
return $this->hasOne(Letter::class);
}
}
class Letter extends Model
{
public function records(): hasMany
{
return $this->hasMany(Record::class);
}
}