mostafijartisan/wherefilter

The package allows developers to dynamically apply `where` and `whereLike` filters to Eloquent queries without writing repetitive query-building logic in their controllers.

v0.1.1 2024-11-28 07:28 UTC

This package is auto-updated.

Last update: 2025-06-28 08:41:27 UTC


README

The package allows developers to dynamically apply where and whereLike filters to Eloquent queries without writing repetitive query-building logic in their controllers.

Instead of explicitly writing ->where() or ->whereLike() statements for each field, you define an array of filters in the model, and the package automatically applies those filters based on the request data.

Installation

composer require mostafijartisan/wherefilter

How it works

  1. Model class config:
  • You have to import Filterable trait in your desire model class.

  • Then you have to create a $whereFilters property that defines which fields can be filtered and how (e.g., where, whereLike).

  • Code Example:

    <?php
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use Mostafijartisan\WhereFilter\Traits\Filter;
    
    class Product extends Model
    {
    
        use Filter;
    
        protected $whereFilters = [
          [
              'request' => 'name',
              'relation' => '',
              'column' => 'name',
              'query' => 'whereLike'
          ],
          [
              'request' => 'color',
              'relation' => 'color',
              'column' => 'color',
              'query' => 'where'
          ],
          [
              'request' => 'status',
              'relation' => '',
              'column' => 'status',
              'query' => 'where'
          ],
          [
              'request' => 'ids',
              'relation' => '',
              'column' => 'id',
              'query' => 'whereIn'
          ],
      ];
    }
  1. Controller class config:
  • In the controller, you simply pass the request data to the query using the scope:
  • whereFilter scope already defined in trait of package and this trait already used in model class. that's why we will can access whereFilter scope from controller.
    $query = Model::query();
    $query->whereFilter($request->all());

Advantages

  1. Reduces Repetition:
  • No need to write individual where or whereLike statements for each field.
  1. Scalable:
  • Adding new filters is as simple as updating the $filters array in the model.
  1. Clean Code:
  • Query-building logic is separated from controllers, making the code easier to read and maintain.
  1. Reusability:
  • The package works across multiple models without duplicating logic.

Behind the Scenes

Here’s a quick breakdown of what's happening at each layer:

Layer Role
Model Defines the $filters array and provides a scopeWhereFilter method to use the package.
Package Implements the whereFilter method, which applies the filters dynamically.
Controller Simplifies query-building by calling the scope directly.
Request Provides the input data used for filtering the query.