Skip to content

This is package to use query filter for each model. This package is made entirely for personal use. But if you are eager to use this, you can easily use this package by using readme. This is very simple and amateur :).

License

Notifications You must be signed in to change notification settings

milito-78/laravel-query-filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Latest Stable Version Total Downloads License

Laravel Query filter package

A simple package for adding your query filter files to project.

Introduction

Install the package with composer using the following command:

composer require milito/query-filter

Usage

For create new query filter you can run this command in your terminal:

php artisan make:query-filter {your-query-filter-name}

Example:

php artisan make:query-filter ProductsFilter

This command will create a ProductsFilter.php file in app/Filters/ path.

This ProductsFilter class, is extended from QueryFilter class, and it used for controller functions. And because of that, constructor of this class requires a request object.

But if you want to create a filter class with a simple array input, you need to use -a|--array option after your class name.

php artisan make:query-filter ProductsFilter -a

This ProductsFilter class, is extended from ArrayQueryFilter.

Namespace

You can add namespace to your file:

php artisan make:query-filter Products/ProductsFilter

The above will create a Products directory inside the app/Filters directory.

How to use!

After file generated you should add your functions.
Example:

<?php

namespace App\Filters;

use Milito\QueryFilter;

class ProductsFilter extends QueryFilter
{
   public function name($name = null)
   {
      if (!$name)
        return $this->builder;
      return $this->builder->where('name' , 'LIKE' , $name);
   }
}

HINT: If your function parameters don't have default values, you need to fill with value every time you want to use it.

Then you need to add QueryFilterScope to your model.
Example :

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Milito\QueryFilterScope;

class Product extends Model{
    use QueryFilterScope;
}

Now you can use in your controller :

<?php

namespace App\Controllers;

use App\Filters\ProductsFilter;
use App\Models\Product;
class ProductsController extends Controller
{
    public function index(ProductsFilter $filter)
    {
       $products = Product::filter($filter)->paginate(20);

       return $products;
    }
}

or :

<?php

namespace App\Controllers;

use App\Filters\ProductsFilter;
use App\Models\Product;
use Illuminate\Http\Request;

class ProductsController extends Controller
{
    public function index(Request $request)
    {
       $filter = new ProductsFilter($request);

       $products = Product::filter($filter)->paginate(20);

       return $products;
    }
}

You can use request() function to get request from filter if you need request.
Example:

//...

  public function index(ProductsFilter $filter)
  {
     $request = $filter->request();

     $products = Product::filter($filter)->paginate($request->per_page??20);

     return $products;
  }

//...

To use a class that inherits from ArrayQueryFilter:

//...

  public function index(Request $request)
  {
     $filter = new ProductsFilter(["name" => $request->query("your_name_field")]);

     $products = Product::filter($filter)->paginate(20);

     return $products;
  }

//...

License

The Milito Query Filter package is open-sourced package licensed under the MIT license.

About

This is package to use query filter for each model. This package is made entirely for personal use. But if you are eager to use this, you can easily use this package by using readme. This is very simple and amateur :).

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages