Closed mtracz closed 5 months ago
This PR fixes lazy loading user relation in VacationRequest model in queued jobs.
user
VacationRequest
Sentry reports:
Illuminate\Database\LazyLoadingViolationException Attempted to lazy load [user] on model [Toby\Models\VacationRequest] but lazy loading is disabled.
Places where user relation is lazy loaded
Update:
Laravel is able to restore single model relations in queued job during __unserialize(). Relations in the collection are not restored by design, see:
__unserialize()
https://github.com/laravel/framework/blob/11.x/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php#L61-L62
Relations restored in single model: https://github.com/laravel/framework/blob/11.x/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php#L109
Relations not restored in collection: https://github.com/laravel/framework/blob/11.x/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php#L81
In this case we need to load required relations manually in queued job after it is unserialized from the queue. Due to each channel (toMail(), toSlack()) is a separate job, we have to load relations for each one.
toMail()
toSlack()
Tested locally and it works.
Code owner review is required.
This PR fixes lazy loading
user
relation inVacationRequest
model in queued jobs.Sentry reports:
Places where
user
relation is lazy loadedUpdate:
Laravel is able to restore single model relations in queued job during
__unserialize()
. Relations in the collection are not restored by design, see:https://github.com/laravel/framework/blob/11.x/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php#L61-L62
Relations restored in single model: https://github.com/laravel/framework/blob/11.x/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php#L109
Relations not restored in collection: https://github.com/laravel/framework/blob/11.x/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php#L81
In this case we need to load required relations manually in queued job after it is unserialized from the queue. Due to each channel (
toMail()
,toSlack()
) is a separate job, we have to load relations for each one.Tested locally and it works.