1. Go to this page and download the library: Download jerome/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/ */
jerome / filterable example snippets
'providers' => [
// Other service providers...
Filterable\Providers\FilterableServiceProvider::class,
],
namespace App\Filters;
use Filterable\Filter;
use Illuminate\Database\Eloquent\Builder;
class PostFilter extends Filter
{
protected array $filters = ['status', 'category'];
protected function status(string $value): Builder
{
return $this->builder->where('status', $value);
}
protected function category(int $value): Builder
{
return $this->builder->where('category_id', $value);
}
}
namespace App\Filters;
use Filterable\Filter;
class PostFilter extends Filter
{
protected array $filters = ['last_published_at'];
protected function lastPublishedAt(int $value): Builder
{
return $this->builder->where('last_published_at', $value);
}
}
namespace App\Models;
use Filterable\Interfaces\Filterable as FilterableInterface;
use Filterable\Traits\Filterable as FilterableTrait;
use Illuminate\Database\Eloquent\Model;
class Post extends Model implements FilterableInterface
{
use FilterableTrait;
}
use App\Models\Post;
$filter = new PostFilter(request(), cache());
$posts = Post::filter($filter)->get();
use App\Models\Post;
use App\Filters\PostFilter;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index(Request $request, PostFilter $filter)
{
$query = Post::filter($filter);
$posts = $request->has('paginate')
? $query->paginate($request->query('per_page', 20))
: $query->get();
return response()->json($posts);
}
}
use App\Models\Post;
use App\Filters\PostFilter;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index(Request $request, PostFilter $filter)
{
$filter->forUser($request->user());
$query = Post::filter($filter);
$posts = $request->has('paginate')
? $query->paginate($request->query('per_page', 20))
: $query->get();
return response()->json($posts);
}
}
use App\Models\Post;
use App\Filters\PostFilter;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Builder;
class PostController extends Controller
{
public function index(Request $request, PostFilter $filter)
{
$filter->registerPreFilters(function (Builder $query) {
return $query->where('published', true);
});
$query = Post::filter($filter);
$posts = $request->has('paginate')
? $query->paginate($request->query('per_page', 20))
: $query->get();
return response()->json($posts);
}
}
/**
* Indicates if caching should be used.
*
* @var bool
*/
protected static bool $useCache = false;
// AppServiceProvider.php
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
// Enable caching globally through methods...
Filter::enableCaching();
}
// AppServiceProvider.php
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
// Disable caching globally through methods...
Filter::disableCaching();
}
namespace App\Filters;
use Filterable\Filter;
class PostFilter extends Filter
{
protected array $filters = ['last_published_at'];
protected function lastPublishedAt(int $value): Builder
{
return $this->builder->where('last_published_at', $value);
}
}
$filter = new PostFilter(request(), cache());
// Control caching
$filter->setCacheExpiration(1440); // Cache duration in minutes
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// Create a logger instance
$logger = new Logger('name');
$logger->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));
// Set the logger to the filter class
$filter->setLogger($logger);
// In a service provider or similar setup
$this->app->when(Filter::class)
->needs(LoggerInterface::class)
->give(function () {
return new Logger('name', [new StreamHandler('path/to/your.log', Logger::WARNING)]);
});
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// Create a logger instance for the Filter class with a custom channel
$logger = new Logger('filter');
$logger->pushHandler(new StreamHandler(storage_path('logs/filter.log'), Logger::DEBUG));
// Set the logger to the filter class
$filter->setLogger($logger);
use Illuminate\Support\Facades\Log;
// In your AppServiceProvider or wherever you set up the Filter class
$filter->setLogger(Log::channel('filter'));
// AppServiceProvider.php
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
// Enable logging globally through methods...
Filter::enableLogging();
}
// AppServiceProvider.php
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
// Disable logging globally through methods...
Filter::disableLogging();
}
public function customFilter($value) {
if (self::shouldLog()) {
$this->getLogger()->info("Applying custom filter with value: {$value}");
}
// Filter logic here
}
if (Filter::shouldLog()) {
$this->getLogger()->info('Performing an important action');
}
namespace Tests\Unit;
use Tests\TestCase;
use App\Models\Post;
use App\Filters\PostFilter;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
class PostFilterTest extends TestCase
{
use RefreshDatabase;
public function testFiltersPostsByStatus(): void
{
$activePost = Post::factory()->create(['status' => 'active']);
$inactivePost = Post::factory()->create(['status' => 'inactive']);
$filter = new PostFilter(new Request(['status' => 'active']));
$filteredPosts = Post::filter($filter)->get();
$this->assertTrue($filteredPosts->contains($activePost));
$this->assertFalse($filteredPosts->contains($inactivePost));
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.