exyplis / eloquent-builder-macros
A set of helpful macros for Laravel Eloquent query builder
Installs: 19 229
Dependents: 0
Suggesters: 0
Security: 0
Stars: 29
Watchers: 7
Forks: 2
Open Issues: 1
pkg:composer/exyplis/eloquent-builder-macros
Requires
- illuminate/database: >=5.4 < 9.0
- illuminate/support: >=5.4 < 9.0
Requires (Dev)
Suggests
- spatie/laravel-collection-macros: A set of useful Laravel collection macros.
README
This package contains few helpful Eloquent builder macros, curated by Exyplis devs team. We find out this ones useful in day-to-day development, since we ❤️ clean, readable and maintainable code.
Compatible with Laravel v5.4+.
P.S. If you have any useful macro for Laravel Eloquent Builder, which is not presented here, feel free to add it to our collection, we appreciate your support and gladly merge it 🤝
Installation
You can install the package via composer:
composer require exyplis/eloquent-builder-macros
Laravel 5.4
Add this entry to providers array in your config/app.php file.
Exyplis\EloquentBuilderMacros\EloquentBuilderMacrosServiceProvider::class
Laravel 5.5+
The package will automatically register itself, so you don't need to do anything else.
Available macros
notEmptyWherenotEmptyOrWherenotEmptyWhereInnotEmptyOrWhereInnotEmptyWhereNotInnotEmptyOrWhereNotInnotEmptyOrWhereTimenotEmptyOrWhereDatenotEmptyOrWhereMonthnotEmptyOrWhereYearifaddSubSelectorderBySuborderBySubDescsearchIn
notEmptyWhere
Check is passed parameter empty, and if not, adds where condition on $column to exiting query.
Useful when you have complex query, with a lot of constructions like
Signature:
notEmptyWhere($column,$param)
Example:
- Model::when($request->has('key'), function($query){ - return $query->where('column',$request->input('key'); - })->get(); + Model::notEmptyWhere('column',$request->input('key'))->get();
notEmptyOrWhere
Check is passed parameter empty, and if not, adds orWhere condition on $column to exiting query.
Useful when you have complex query, with a lot of constructions like
Signature:
notEmptyOrWhere($column,$param)
Example:
- Model::when($request->has('key'), function($query){ - return $query->orWhere('column',$request->input('key'); - })->get(); + Model::notEmptyOrWhere('column',$request->input('key'))->get();
notEmptyWhereIn
Check is passed parameter empty, and if not, adds whereIn condition on $column to exiting query.
In this case, $param should be array.
Signature:
notEmptyWhereIn($column,$params)
Example:
- Model::when($request->has('user_ids'), function($query){ - return $query->whereIn('user_id', $request->input('user_ids'); - })->get(); + Model::notEmptyWhereIn('column',$request->input('user_ids'))->get()
notEmptyOrWhereIn
Check is passed parameter empty, and if not, adds orWhereIn condition on $column to exiting query.
In this case, $param should be array.
Signature:
notEmptyOrWhereIn($column,$params)
Example:
- Model::when($request->has('user_ids'), function($query){ - return $query->orWhereIn('user_id', $request->input('user_ids'); - })->get(); + Model::notEmptyOrWhereIn('column',$request->input('user_ids'))->get()
notEmptyWhereNotIn
Check is passed parameter empty, and if not, adds whereNotIn condition on $column to exiting query.
In this case, $param should be array.
Signature:
notEmptyWhereNotIn($column,$params)
Example:
- Model::when($request->has('user_ids'), function($query){ - return $query->whereNotIn('user_id', $request->input('user_ids'); - })->get(); + Model::notEmptyWhereNotIn('column',$request->input('user_ids'))->get()
notEmptyOrWhereNotIn
Check is passed parameter empty, and if not, adds orWhereNotIn condition on $column to exiting query.
In this case, $param should be array.
Signature:
notEmptyOrWhereNotIn($column,$params)
Example:
- Model::when($request->has('user_ids'), function($query){ - return $query->orWhereNotIn('user_id', $request->input('user_ids'); - })->get(); + Model::notEmptyOrWhereNotIn('column',$request->input('user_ids'))->get()
notEmptyWhereTime
Check is passed parameter empty, and if not, adds whereTime condition on $column to exiting query.
Useful when you have complex query, with a lot of constructions like
Signature:
notEmptyWhereTime($column,$param)
Example:
- Model::when($request->has('key'), function($query){ - return $query->whereTime('column',$request->input('key'); - })->get(); + Model::notEmptyWhereTime('column',$request->input('key'))->get();
notEmptyWhereDate
Check is passed parameter empty, and if not, adds whereDate condition on $column to exiting query.
Useful when you have complex query, with a lot of constructions like
Signature:
notEmptyWhereDate($column,$param)
Example:
- Model::when($request->has('key'), function($query){ - return $query->whereDate('column',$request->input('key'); - })->get(); + Model::notEmptyWhereDate('column',$request->input('key'))->get();
notEmptyWhereMonth
Check is passed parameter empty, and if not, adds whereMonth condition on $column to exiting query.
Useful when you have complex query, with a lot of constructions like
Signature:
notEmptyWhereMonth($column,$param)
Example:
- Model::when($request->has('key'), function($query){ - return $query->whereMonth('column',$request->input('key'); - })->get(); + Model::notEmptyWhereMonth('column',$request->input('key'))->get();
notEmptyWhereYear
Check is passed parameter empty, and if not, adds whereYear condition on $column to exiting query.
Useful when you have complex query, with a lot of constructions like
Signature:
notEmptyWhereYear($column,$param)
Example:
- Model::when($request->has('key'), function($query){ - return $query->whereYear('column',$request->input('key'); - })->get(); + Model::notEmptyWhereYear('column',$request->input('key'))->get();
if
Check passed condition, and adds custom where clause to query, when condition returns true.
Signature:
if($condition, $column, $operator, $value)
Example:
- Model::when($request->customer_id, function($query) use ($request){ - return $query->where('customer_id', $request->customer_id); - })->get(); + Model::if($request->customer_id, 'customer_id', '=', $request->customer_id)->get()
addSubSelect
Signature:
// this should be documented.
Example:
-Before +After
orderBySub
Signature:
// this should be documented
Example:
-Before +After
orderBySubDesc
Signature:
// this should be documented.
Example:
-Before +After
searchIn
Signature:
searchIn($attributes, $needle)
Example:
// Get row, where name or email contains `john` Model::search(['name','email',], 'john')->get();
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email bks@exyplis.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.