Making the advanced search query with Eloquent-Builder in Laravel

mohammadfouladgar

Fouladgar.dev

Posted on September 5, 2018

Making the advanced search query with Eloquent-Builder in Laravel

Unsplash @aginsbrook
We needed an advanced search system in the recent project. This system included many filters that required a flexible and scalable search system.
I decided to implement a package for this system that you can see it in the GitHub and use it in your project.

What's the problem?

The problem is that you will encounter with a set of filters that you must check for a lot of conditions to add to the query.

Writing a lot of terms will surely reduce the readability of your code and slow down the development process.

Also, you can only use the filters and terms in the same scope and they will not be reusable.

But the Solution

You must Refactor your code!

To solve this problem, you need to Refactor your code by replacing many of your conditionals with Polymorphism.

Click here to learn more about this design pattern.

Practical Example:

Suppose we want to get the list of the users with the requested parameters as follows:

http://dev.to/api/users/search?age_more_than=25&gender=male&has_published_post=true
Enter fullscreen mode Exit fullscreen mode

The Requested parameter will be as follows:

[ 
  'age_more_than' => '25',
  'gender' => 'male',
  'has_published_post' => 'true',
]
Enter fullscreen mode Exit fullscreen mode

In a common implementation, following code will be expected:

We check out a condition for each request parameter.

In the future, we will have more parameters that should be checked and applied in the code above that causes us to have a dirty code.

Use the Eloquent-Builder

After installing the package presented,change your code as follows:

You just send the model and parameters to ‘to’ method.Then, you need to define the filter for each parameter that you want to add to the query.

So easy!

Defining a filter

For example, I’ll implement one of the above example filters. Follow the example below:


For more details check out the GitHub repository.

GitHub logo mohammad-fouladgar / eloquent-builder

This package provides an advanced filter for Laravel model based on incoming requets.

Provides a Eloquent query builder for Laravel

alt text

Latest Version on Packagist Test Status Code Style Status Total Downloads

This package allows you to build eloquent queries, based on incoming request parameters. It greatly reduces the complexity of the queries and conditions, which will make your code clean and maintainable.

Version Compatibility

Laravel EloquentBuilder
11.0.x 5.x.x
10.0.x 4.2.x
9.0.x 4.0.x
6.0.x to 8.0.x 3.0.x
5.0.x 2.2.2

Basic Usage

Suppose you want to get the list of the users with the requested parameters as follows:

//Get api/user/search?age_more_than=25&gender=male&has_published_post=true
[
    'age_more_than'  => '25',
    'gender'         => 'male',
    'has_published_post' => true,
]
Enter fullscreen mode Exit fullscreen mode

In a common implementation, following code will be expected:

<?php
namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index(Request $request)
    {
        $users = User::where(
Enter fullscreen mode Exit fullscreen mode

Good luck and thank you for sharing your valuable time with me.

Happy Coding!

💖 💪 🙅 🚩
mohammadfouladgar
Fouladgar.dev

Posted on September 5, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related