I have a model, Project, that I am eager loading relations in the Http/Controller.
The problem is that the withDefault closure on the belongsTo Relation in the model always fires, regardless of if there is an actual relation to eager load, resulting in many pointless queries and strange side effects.
This doesn't seem like it should be expected behaviour - is there any way to stop it calling withDefault() unless it actually needs to?
To clarify:
return $project->loadMissing('address');
From the model:
public function address(): BelongsTo
{
return $this
->belongsTo(Address::class, 'address_id')
->withDefault(function() {
// This will always trigger
});
}
withDefault triggers always, regardless of the state of the actual relation.
Looks like it is the default behavior.
The callback function inside
withDefaultfunction will get executed every time Laravel sets theBelongsTorelation of a model.Take a look at the initRelation function of laravel's core
BelongsToclassAnd if you look at the
getDefaultForandwithDefaultfunctions hereYou will observe that if
$this->withDefaultis a callable, it will always be executed; there isn't a direct way to conditionally trigger its execution.I would recommend avoiding running heavy tasks like database queries inside that function, as it could lead to performance problems, especially when processing a large amount of data.