Skip to content

A set of helpful macros for Laravel Eloquent query builder

License

Notifications You must be signed in to change notification settings

freekmurze/eloquent-builder-macros

Repository files navigation

Eloquent builder macros

Latest Stable Version Latest Unstable Version Total Downloads License StyleCI

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

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();

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()

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()
Signature:
// this should be documented.
Example:
-Before
+After
Signature:
// this should be documented
Example:
-Before
+After
Signature:
 // this should be documented.
Example:
-Before
+After
Signature:
 searchIn($attrtibutes, $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.

About

A set of helpful macros for Laravel Eloquent query builder

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages