signifly / laravel-builder-macros
A set of useful Laravel builder macros.
Installs: 10 890
Dependents: 1
Suggesters: 0
Security: 0
Stars: 28
Watchers: 3
Forks: 4
Open Issues: 0
Requires
- php: ^7.2.5|^8.0
- illuminate/database: ^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ^4.0|^5.0|^6.0|^7.0|^8.0
- phpunit/phpunit: ^7.0|^8.0|^9.0
README
The signifly/laravel-builder-macros
package allows you to easily add a set of useful builder macros to your Laravel app.
Installation
You can install the package via composer:
composer require signifly/laravel-builder-macros
The package will automatically register itself.
Macros
addSubSelect
Add a select sub query.
// Params: $column, $query $query->addSubSelect('primary_address_id', Address::select('id') ->where('user_id', $user->id) ->primary() ); // It adds primary_address_id to the result set
defaultSelectAll
It selects all columns from the query. Useful for queries with joins and additional selects.
$query->defaultSelectAll() ->join('contacts', 'users.id', '=', 'contacts.user_id') ->addSelect('contacts.name as contact_name');
joinRelation
A query way to join relations.
// Params: $relationName, $operator $query->joinRelation('contact');
leftJoinRelation
A query to left join relations.
// Params: $relationName, $operator $query->leftJoinRelation('contact');
map
A direct method to retrieve the results and map it.
$userIds = $query->where('user_id', 10)->map(function ($user) { return $user->id; }); // Returns a collection
whereLike
Search in your models with the LIKE
operator.
$query->whereLike('title', 'john')->get(); // Returns all results where title includes `john`
You can also supply an array of columns to search in:
$query->whereLike(['title', 'contact.name'], 'john')->get(); // Returns all results where title or contact.name includes `john`
Testing
composer test
Security
If you discover any security issues, please email dev@signifly.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.