Download the PHP package clickbar/laravel-custom-relations without Composer
On this page you can find all versions of the php package clickbar/laravel-custom-relations. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download clickbar/laravel-custom-relations
More information about clickbar/laravel-custom-relations
Files in clickbar/laravel-custom-relations
Package laravel-custom-relations
Short Description This package provides functionality for custom relations in laravel
License MIT
Homepage https://github.com/clickbar/laravel-power-relations
Informations about the package laravel-custom-relations
Laravel Custom Relations
Laravel provides some pretty good relations from scratch. If those relations do not fit the need, the community has provided a lot of other relation packages. Especially staudenmeir did a lot if nice work on that end. However, in our projects we encountered the need of some really custom relations that could easily be expressed with an eloquent query, but not with default relations. Therefore, we created this package to give you the full control over your relations.
Introduction
Sometimes it needs to be more custom. This package extends the default Laravel Relations with Relations that can be described by a query.
For explanation purposes we consider the following model with a simple concatenated relation use case. (for cases like this you should also have a look eloquent-has-many-deep from staudenmeir). Even the example is quite simple, it should be able to represent the huge amount of possibilities that comes with custom relations.
Let's explorer the connection between Client and Task in the default Laravel way:
Wouldn't it be cool to do stuff like this with only 1 single database query?
With Custom Relations you can do this with only one single Database Query.
Installation
You can install the package via composer:
Preparing the Model
In order to use the Custom Relations, you must use the HasCustomRelation
trait:
Writing the Relation
Like you know from Laravel, relations can return a collection of models or just one model.
Therefore, this package has two Different Relations CustomRelation
and CustomRelationSingle
.
Let's look at our two examples from above:
Like regular Laravel Relations, the query builder starts from the related model.
This results in the following join chains:
$task->client: Client->Projects->Orders->Tasks
$client->tasks: Tasks->Orders->Projects->Client
If you prefer starting the join from the model the relation is defined on, you can use the method with the fromParent
suffix:
Limitations
Since the query might introduce a lot of joins, some methods known from Laravel Relations are not available:
- make
- create
- update
- forceCreate
- forceDelete
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Adrian Hawlitschek
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-custom-relations with dependencies
spatie/laravel-package-tools Version ^1.14.0
illuminate/contracts Version ^10.0