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 🤝
You can install the package via composer:
composer require exyplis/eloquent-builder-macros
Add this entry to providers
array in your config/app.php
file.
Exyplis\EloquentBuilderMacros\EloquentBuilderMacrosServiceProvider::class
The package will automatically register itself, so you don't need to do anything else.
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
notEmptyWhere($column,$param)
- Model::when('$request->has('key'), function($query){
- return $query->where('column',$request->input('key');
- })->get();
+ Model::notEmptyWhere('column',$request->input('key'))->get();
Check is passed parameter empty, and if not, adds whereIn
condition on $column
to exiting query.
In this case, $param
should be array.
notEmptyWhereIn($column,$params)
- 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()
Check passed condition, and adds custom where
clause to query, when condition returns true
.
if($condition, $column, $operator, $value)
- 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()
// this should be documented.
-Before
+After
// this should be documented
-Before
+After
// this should be documented.
-Before
+After
searchIn($attrtibutes, $needle)
// Get row, where name or email contains `john`
Model::search(['name','email',], 'john')->get();
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email bks@exyplis.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.