Download the PHP package dedmytro/laravel-model-related-fields without Composer
On this page you can find all versions of the php package dedmytro/laravel-model-related-fields. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dedmytro/laravel-model-related-fields
More information about dedmytro/laravel-model-related-fields
Files in dedmytro/laravel-model-related-fields
Package laravel-model-related-fields
Short Description Provide an ability to add related fields/columns to all model Eloquent queries
License MIT
Informations about the package laravel-model-related-fields
Laravel Model Related Fields
Provide an ability to add related fields/columns to all model Eloquent queries without additional database queries. It uses sub SELECT and joins so take into account extra database load when database has a lot of records.
Table of contents
- Installation
- Using
Installation
To get the latest version of Laravel Model Related Fields, simply require the project using Composer:
Or manually update require block of composer.json and run composer update.
Using
Add HasRelatedFields trait to your model and define method addRelatedFields()
which returns array related fields you want to load on each Model query.
On example below you can see how to load related field through many relations and load SUM() of HasMany relation.
where
'event.company.country.currency' is the same as $order->event->company->country->currency
'items.total' is the same as $order->items()->sum('price')
As an alternative you can use $relatedFields property for simple fields, without RelatedField class usage. Property
and method result array will be be merged.
The result of query:
you will get Order model with all fields plus additional related fields:
How to disable/enable
Disable globally
To disable related fields for all models by default,
-
publish config and change default value
- add env variable
Disable for particular model
Add protected $enableRelatedFields = false