PHP code example of coderscantina / laravel-filter

1. Go to this page and download the library: Download coderscantina/laravel-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/ */

    

coderscantina / laravel-filter example snippets


 namespace App;
 
use CodersCantina\Filter\ExtendedFilter;
 
class TestFilter extends ExtendedFilter
{
    public function name($name)
    {
        return $this->builder->where('name', $name);        
    }
    
    public function latest()
    {
        return $this->builder->latest();        
    }
}

 namespace App;
 
use Illuminate\Database\Eloquent\Model;
use CodersCantina\Filter\Filterable;
 
class TestModel extends Model
{
    use Filterable;
}

 namespace App\Http\Controllers;
 
use App\TestModel;
use App\TestFilter;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Collection;
 
class LessonsController extends Controller
{
    /**
     * Show all lessons.
     *
     * @param  Request $request
     * @return Collection
     */
    public function index(Request $request)
    {
        $filter = new TestFilter($request->all());
        
        // For enhanced security, whitelist allowed filters
        $filter->setWhitelistedFilters(['name', 'latest']);

        return TestModel::filter($filter)->get();
    }
}

 namespace App;
 
use CodersCantina\Filter\AdvancedFilter;
 
class ProductFilter extends AdvancedFilter
{
    public function name($value)
    {
        // Automatically handles operator syntax
        $this->applyDynamicFilter('name', $value);
    }
    
    public function price($value)
    {
        // Supports both operators and range syntax
        $this->applyAdvancedRangeFilter('price', $value);
    }
    
    public function created_at($value)
    {
        // Supports both operators and date range syntax
        $this->applyAdvancedDateFilter('created_at', $value);
    }
}

// Find products with names containing "phone"
$filter = new ProductFilter(['name' => 'like:phone']);

// Find products with price greater than or equal to 100
$filter = new ProductFilter(['price' => 'gte:100']);

// Find products created after January 1, 2023
$filter = new ProductFilter(['created_at' => 'gte:2023-01-01']);

// Find products in specific categories
$filter = new ProductFilter(['category' => 'in:electronics,phones,accessories']);

// Find products that are either active or featured
$filter = new ProductFilter(['status' => ['eq:active', 'eq:featured']]);

// Find non-empty descriptions
$filter = new ProductFilter(['description' => '!empty:']);

// Traditional range syntax still works
$filter = new ProductFilter(['price' => '100...500']);

// Using operators for precise comparisons
$filter = new ProductFilter(['price' => 'gte:100']);

// Traditional date range
$filter = new ProductFilter(['created_at' => '2023-01-01...2023-12-31']);

// Using operators
$filter = new ProductFilter(['created_at' => 'gte:2023-01-01']);

// Products that are either active or pending
$filter = new ProductFilter([
    'status' => ['eq:active', 'eq:pending']
]);

$filter = new ProductFilter(['price' => 'between:10,50']);
$filter->setCustomOperators(['between' => 'BETWEEN']);

 namespace App;
 
use CodersCantina\Filter\ExtendedFilter;
use CodersCantina\Filter\AdvancedFilterable;
 
class CustomFilter extends ExtendedFilter
{
    use AdvancedFilterable;
    
    public function status($value)
    {
        $this->applyDynamicFilter('status', $value);
    }
}

$filter->setWhitelistedFilters(['name', 'price', 'category', 'status']);

$filter->setWhitelistedFilters(['name', 'price', 'category']);

['sort' => '+foo,-bar']; // -> order by foo asc, bar desc

['sort' => '+foo.bar']; // -> left join x on x.id = foo.id order by foo.bar asc

$filter->setMaxSortColumns(3);

protected array $sortableColumns = ['name', 'price', 'created_at'];

['price' => '10...']; // -> price >= 10
['price' => '...50']; // -> price <= 50
['price' => '10...50']; // -> price >= 10 and price <= 50

['created_at' => '2023-01-01...']; // -> created_at >= '2023-01-01 00:00:00'
['created_at' => '...2023-12-31']; // -> created_at <= '2023-12-31 23:59:59'
['created_at' => '2023-01-01...2023-12-31']; // -> Between Jan 1 and Dec 31, 2023

['limit' => 10, 'offset' => 20]; // -> LIMIT 10 OFFSET 20

public function active($value = true)
{
    $this->builder->where('active', $value);
}

public function priceRange($value)
{
    $this->applyRangeFilter('price', $value);
}

public function dateCreated($value)
{
    $this->applyDateFilter('created_at', $value);
}

protected function isValidColumnName(string $column): bool
{
    // Your custom validation logic
    return parent::isValidColumnName($column) && in_array($column, $this->allowedColumns);
}