blasttech / eloquent-related-plus
Adds search and order functionality to Laravel Eloquent related models
Installs: 28 038
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 11
Forks: 0
Open Issues: 0
Requires
- php: >=7.0
- illuminate/database: ~5.3.0|~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8|~6.0|~7.0|~8.0
- illuminate/support: ~5.3.0|~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8|~6.0|~7.0|~8.0
Requires (Dev)
- orchestra/database: ~3.3.0|~3.4.0|3.5.0
- orchestra/testbench: ~3.3.0|~3.4.0|3.5.0
- phpunit/phpunit: ^5.7|^6.2|^7.0
- dev-master
- 1.0.1
- 1.0.0
- 0.4.11
- 0.4.10
- 0.4.9
- 0.4.8
- 0.4.7
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.33
- 0.3.32
- 0.3.31
- 0.3.30
- 0.3.29
- 0.3.28
- 0.3.27
- 0.3.26
- 0.3.25
- 0.3.24
- 0.3.23
- 0.3.22
- 0.3.21
- 0.3.20
- 0.3.19
- 0.3.18
- 0.3.17
- 0.3.16
- 0.3.15
- 0.3.14
- 0.3.13
- 0.3.12
- 0.3.11
- 0.3.10
- 0.3.9
- 0.3.8
- 0.3.7
- 0.3.6
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-update-laravel-80
- dev-change-attributes-camelcase
- dev-table-connection
- dev-update-laravel-60
- dev-update-laravel-58
- dev-fix-nested-where-on-relation
- dev-add-search-fields-getters-setters
- dev-update-composer
This package is auto-updated.
Last update: 2024-12-14 00:34:46 UTC
README
This package provides a trait that adds extra ways to order and search complex models. With Eloquent, when relations are used (eg. HasOne, HasMany, BelongsTo) the primary model can't be sorted using a field in a related table.
For example, you might have a list of Customers with a one-to-one relation (HasOne) to a Contacts model for the Customer contact.
class Customer extends Eloquent { public function sales_rep() { return $this->hasOne(Contact::class, 'id', 'sales_rep_id'); } }
If you had a table of customers and wanted to sort it by Contact name, you nomrally wouldn't be able to in Laravel using the relation above, but you could with this package.
Installation
This package can be installed through Composer.
$ composer require blasttech/eloquent-related-plus
Usage
To add complex order and search behaviour to your model you must:
- specify that the model will conform to
Blasttech\EloquentRelatedPlus\RelatedPlusInterface
- use the trait
Blasttech\EloquentRelatedPlus\RelatedPlusTrait
Using the earlier example:
use App\Contact; use Blasttech\EloquentRelatedPlus\RelatedPlusInterface; use Blasttech\EloquentRelatedPlus\RelatedPlusTrait; class Customer extends Eloquent implements RelatedPlusInterface { use RelatedPlusTrait; public function sales_rep() { return $this->hasOne(Contact::class, 'id', 'sales_rep_id'); } }
modelJoin
Use this scope to add a join for a related model.
- modelJoin($relation_name, $operator = '=', $type = 'left', $where = false, $related_select = true)
Example
namespace App\Http\Controllers; use App\Customer; class CustomerController extends Controller { public function getCustomers() { return Customer::select('*') ->modelJoin('Contacts', '=', 'left', false, false) ->get(); } ... }
This will add a left join to the Contacts model using the Contacts() relation, using 'where' instead of 'on' and include all the fields from the model in the select.
$relation_name
The name of the relation. Only BelongsTo, HasOne, HasMany or HasOneOrMany relations will work.
$operator
The operator used to join the related table. This will default to '=' but any of the standard Laravel join operators can be used.
$type
The type of join. This will default to 'left' but any of the standard Laravel join types are allowed.
$where
The method of joining the table. The default (false) uses an 'on' statement but if true, a where statement is used.
$related_select
The $related_select option determines if the fields in the joined table will be included in the query. If true, the field names will be in the format 'table_name.column_name', for example, 'customer.contact_name' with the table name and period (.) included in the field name. This is to allow fields from joined tables to be used when they have the same column names.
orderByCustom
- orderByCustom($orderField, $dir, $orderFields = null, $orderDefaults = null)
Example
use App\Customer; use Blasttech\EloquentRelatedPlus\RelatedPlusInterface; use Blasttech\EloquentRelatedPlus\RelatedPlusTrait; class CustomerController extends Controller { public function getCustomers() { return Customer::select('*') ->modelJoin('Contacts', '=', 'left', false, false) ->orderByCustom('contact_name'); } ... }
This will add a left join to the Contacts model using the Contacts() relation and then sort it by the contact_name.
search
Example
use Blasttech\EloquentRelatedPlus\RelatedPlusInterface; use Blasttech\EloquentRelatedPlus\RelatedPlusTrait; class MyModel extends Eloquent implements RelatedPlusInterface { use RelatedPlusTrait; public function getContact($contact_name) { return Customer::select('*') ->modelJoin('Contacts', '=', 'left', false, false) ->search($contact_name); } ... }
This will add a left join to the Contacts model using the Contacts() relation, and search for $contact_name.
License
The MIT License (MIT). Please see License File for more information.
Credits
The example shown in http://laravel-tricks.com/tricks/automatic-join-on-eloquent-models-with-relations-setup was used as a basis for the modelJoin and relationJoin scopes.