Download the PHP package msafadi/laravel-eloquent-join-with without Composer
On this page you can find all versions of the php package msafadi/laravel-eloquent-join-with. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download msafadi/laravel-eloquent-join-with
More information about msafadi/laravel-eloquent-join-with
Files in msafadi/laravel-eloquent-join-with
Package laravel-eloquent-join-with
Short Description Laravel Eloquent Join With Relationships
License MIT
Informations about the package laravel-eloquent-join-with
Laravel Eloquent JoinWith
Laravel Eloquent Join With is a package that simplifies performing efficient database joins on existing Eloquent relationships of type HasOne
and BelongsTo
. By utilizing these relationships, JoinWith optimizes performance by executing a single query instead of the two separate queries typically required with the standard with
method. This translates to faster and more efficient data retrieval.
Installation
You can install the package via composer:
Usage
There are two ways to use Laravel JoinWith in your application models:
1. Use JoinWith
Trait
Include the JoinWith
trait provided by the package in your application models:
With the trait included, you can then use the joinWith
method directly on your model queries.
2. Extend the Model Class
Alternatively, you can extend your model classes with Safadi\EloquentJoinWith\Database\Eloquent\Model
:
This approach also grants access to the joinWith
method on your model queries.
Usage Examples
Once you've integrated Laravel JoinWith into your models, you can use the joinWith
method on your Eloquent model queries.
The defintion and usage of joinWith
is exactly the same as using the standard with
method. But it will execute a single query joining the parent model and the realted model tables instead of fetching realted models with executing an extra database query. Here's an example:
This code retrieves the user information along with the associated profile's avatar in a single query.
As the standard with
method, you can also use nested relations:
For more complex scenarios, you can pass a closure to the joinWith
method to customize the join conditions, similar to the standard with
method:
This example retrieves orders that belongs to a verified user, combining the user and order information in a single query.
Limitations
- Laravel JoinWith currently works with
HasOne
andBelongsTo
relationships. Support for other relationship types might be added in future versions. - Specifying which columns of the relationship you would like to retrieve is not supported yet but might be added in future versions.
Contributing
We welcome contributions to this package! If you'd like to contribute, please feel free to open a pull request.
License
This package is distributed under the MIT License. Please see the LICENSE file for more information.