PHP code example of milito / query-filter

1. Go to this page and download the library: Download milito/query-filter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

milito / query-filter example snippets




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



namespace App\Models;

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

class Product extends Model{
    use QueryFilterScope;
}



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;
    }
}



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;
    }
}

//...

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

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

     return $products;
  }

//...


//...

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

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

     return $products;
  }

//...