Sorting related models in Laravel
There are a number of related models to this one model that I store in the Models $relations property
protected static $relations = [
'owner',
'apns',
'zoning_urls',
'structures.address',
'structures.comments.user.profiles',
'listings',
'attachments',
'structures.structure_type',
'property_owner',
'primary_property_type',
'address',
'property_specialist.profiles',
'listings.team'
];
This allows me to easily reuse this for each query
$found = $found->load(static::$relations);
But there is one model I removed from that list to do a dynamic query/sorting on it
$found = $this->property->with([
'verifiers' => function($query) {
$query->with('user.profiles')->orderBy('id', 'DESC');
},
])->findOrFail($id);
$found = $found->load(static::$relations);
veifiers was part of the protected static $relations list but I moved it out since, for each query, I need to sort them from last to first.
comments powered by Disqus