Download the PHP package stayallive/laravel-inverse-relations without Composer
On this page you can find all versions of the php package stayallive/laravel-inverse-relations. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download stayallive/laravel-inverse-relations
More information about stayallive/laravel-inverse-relations
Files in stayallive/laravel-inverse-relations
Package laravel-inverse-relations
Short Description HasMany, HasOne and MorphMany Eloquent relations with inverse hydration.
License MIT
Homepage https://github.com/stayallive/laravel-inverse-relations
Informations about the package laravel-inverse-relations
Laravel Inverse Relations
HasMany
, HasOne
and MorphMany
Eloquent relations with inverse hydration.
Jonathan Reinink wrote a great blog post about optimizing circular relationships in Laravel.
The package stancl/laravel-hasmanywithinverse ran with that idea and implemented it for the hasMany
relation. This package improves on that by implementing it for the hasOne
and morphMany
relations too.
In short, this package allows you to define inverse relations on your models. This means that you can define a relation on a model that points to another model. This is useful when you have a circular relationship between two models and you want to be able to access the inverse relation without having to load the other model from the database potentially saving a lot of database queries.
Installation
Usage
Adding the HasHasManyWithInverseRelation
, HasHasOneWithInverseRelation
and/or HasMorphManyWithInverseRelation
traits will provide you with the helper methods to setup the inverse relations.
Check out the test stubs for examples on how to implement the traits on your models to setup the inverse relations:
- HasMany
- HasOne
- MorphMany
Caveats
Because the inverse relation stores the parent by reference (not cloned), you need to be careful when you are using the relation since you will be modifying the parent model. This is nothing new to this package since eager loading does the same thing but I thought to mention it since it could catch you off guard.
There is one other downside to the inverse relation being stored as a reference since it creates a circular reference. This means that you cannot recusively serialize the model that has the inverse relation.
Laravel does this for example when you pass the model in a queued job since the SerializesModels
trait will also try to store which relations were loaded when the model was queued to load those relations back causing once the job is being processed.
This causes an infinite loop trying to figure out which relations are loaded recursively, eating up memory until the process is killed by the OS or crashes.
To prevent this you can either pass your model to your job using $model->withoutRelations()
or you can disable the relations loading on your model by adding the following method to your model:
This disables the collection and loading of relations on the model when it (un-)serialized for the queued job.
Personally I put this in my base model since I never want to load the relations loaded when I push a job on the queue since they are mostly not useful in the context of the job and apart from guarding against circular references this also saves database queries (sometimes a lot) retrieving unused relations.
Security Vulnerabilities
If you discover a security vulnerability within this package, please send an e-mail to Alex Bouma at [email protected]
. All security vulnerabilities will be swiftly addressed.
License
This package is open-sourced software licensed under the MIT license.