PHP code example of ysm / filterable

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

    

ysm / filterable example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use YSM\Filterable\Concerns\InteractWithFilterable;

class Post extends Model
{
    use InteractWithFilterable;

    protected $fillable = ['title', 'category', 'published', 'created_at'];
}



namespace App\Http\Filters;

use YSM\Filterable\Filterable;
use Illuminate\Database\Eloquent\Builder;

class PostFilter extends Filterable
{
    protected array $allowedFilters = ['title', 'category', 'published'];

    public function title(string $value): void
    {
        $this->builder->where('title', 'like', "%{$value}%");
    }

    public function category(string $value): void
    {
        $this->builder->where('category', $value);
    }

    public function published(bool $value): void
    {
        $this->builder->where('published', $value);
    }
}



namespace App\Http\Controllers;

use App\Http\Filters\PostFilter;
use App\Models\Post;

class PostController extends Controller
{
    public function index()
    {
        $filter = PostFilter::make();
        $posts = Post::filterable($filter)->get();
        return response()->json(['data' => $posts]);
    }
}



namespace App\Http\Controllers;

use App\Http\Filters\PostFilter;
use App\Models\Post;

class PostController extends Controller
{
    public function index()
    {
        // Pass the filter class name as a string
        $posts = Post::filterable(PostFilter::class)->get();
        return response()->json(['data' => $posts]);
    }
}



namespace App\Http\Filters;

use YSM\Filterable\Filterable;
use Illuminate\Database\Eloquent\Builder;

class PostFilter extends Filterable
{
    protected array $allowedFilters = ['title', 'category', 'published'];
    protected array $aliases = [
        'cat' => 'category', // Maps 'cat' request param to 'category' filter
    ];

    public function title(string $value): void
    {
        $this->builder->where('title', 'like', "%{$value}%");
    }

    public function category(string $value): void
    {
        $this->builder->where('category', $value);
    }

    public function published(bool $value): void
    {
        $this->builder->where('published', $value);
    }
}

$filter = PostFilter::make()->aliases(['cat' => 'category']);
$posts = Post::filterable($filter)->get();



namespace App\Http\Filters;

use YSM\Filterable\Filterable;
use Illuminate\Database\Eloquent\Builder;

class PostFilter extends Filterable
{
    protected array $autoApplyFilters = ['published'];

    public function published(bool $value = true): void
    {
        $this->builder->where('published', $value);
    }
}

$filter = PostFilter::make()->autoApply(['published']);
$posts = Post::filterable($filter)->get();



namespace App\Http\Filters;

use YSM\Filterable\Filterable;
use Illuminate\Database\Eloquent\Builder;

class PostFilter extends Filterable
{
    protected array $allowedFilters = ['title', 'category']; // Whitelist
    protected array $forbiddenFilters = ['created_at']; // Blacklist

    public function title(string $value): void
    {
        $this->builder->where('title', 'like', "%{$value}%");
    }

    public function category(string $value): void
    {
        $this->builder->where('category', $value);
    }

    public function created_at(string $value): void
    {
        $this->builder->whereDate('created_at', $value);
    }
}

$filter = PostFilter::make()->only(['title', 'category'])->except(['created_at']);
$posts = Post::filterable($filter)->get();



namespace App\Http\Filters;

use YSM\Filterable\Filterable;
use Illuminate\Database\Eloquent\Builder;

class PostFilter extends Filterable
{
    protected array $defaults = ['published' => true, 'category' => 'blog'];

    public function category(string $value): void
    {
        $this->builder->where('category', $value);
    }

    public function published(bool $value): void
    {
        $this->builder->where('published', $value);
    }
}

$filter = PostFilter::make()->defaults(['published' => true, 'category' => 'blog']);
$posts = Post::filterable($filter)->get();



namespace App\Http\Controllers;

use App\Http\Filters\PostFilter;
use App\Models\Post;

class PostController extends Controller
{
    public function index()
    {
        $filter = PostFilter::make()->only(['title', 'category']);
        $posts = Post::filterable($filter)->get();
        return response()->json([
            'data' => $posts,
            'applied_filters' => $filter->getAppliedFilters(),
            'configured_filters' => $filter->getConfiguredFilters(),
        ]);
    }
}
bash
   php artisan vendor:publish --provider="YSM\Filterable\FilterableServiceProvider"
   
bash
curl -X GET "http://your-app.test/posts?title=Test&created_at=2025-01-01"