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