PHP code example of lacodix / laravel-model-filter
1. Go to this page and download the library: Download lacodix/laravel-model-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/ */
lacodix / laravel-model-filter example snippets
// Set the filter mode
// App\Models\Filters\CreatedAfterFilter
public FilterMode $mode = FilterMode::GREATER_OR_EQUAL;
// Apply this filter and the HasFilters trait to a Model
// App\Models\Post
use HasFilters;
protected array $filters = [
CreatedAfterFilter::class,
];
// Somwhere in a controller, select all posts created after 1st of January 2023
Post::filter(['created_after_filter' => '2023-01-01'])->get();
// Do the same via query string by calling
// this url: https://.../posts?created_after_filter=2023-01-01
Post::filterByQueryString()->get();
// add searchable fields and the IsSearchable trait to Model:
// App\Models\Post
use IsSearchable;
protected array $searchable = [
'title',
'content',
];
// Somewhere in controller, find all posts that contain "test" in title or content
Post::search('test')->get();
// Treat %, _ and database-specific wildcard characters as ordinary input
Post::searchLiteral('100%_complete')->get();
// Do the same via query string by calling
// this url: https://.../posts?search=test
Post::searchByQueryString()->get();
use Lacodix\LaravelModelFilter\Support\FilterPreparation;
$prepared = (new FilterPreparation)->prepare(
model: new Post,
values: $request->all(),
group: 'backend',
strictGroup: true,
);
foreach ($prepared as $filter) {
// Inspect the fresh, populated filter instance.
}