blumilksoftware / toby

HR software you love to hate
MIT License
17 stars 4 forks source link

- fix lazy loading in VacationRequest model #442

Closed mtracz closed 5 months ago

mtracz commented 5 months ago

This PR fixes lazy loading user relation in VacationRequest model in queued jobs.

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:

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.

mtracz commented 5 months ago

Code owner review is required.