1. Go to this page and download the library: Download timedoor/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/ */
timedoor / laravel-filter example snippets
namespace App\Filters;
class UserFilter
{
public function keyword($builder, $value)
{
//
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Timedoor\LaravelFilter\Concerns\Filterable;
class User extends Model
{
use Filterable;
}
use App\Http\Filters\UserFilter;
$users = User::applyFilter(UserFilter::class)->get();
// app/Http/Filters/UserFilter.php
namespace App\Filters;
class UserFilter
{
public function name($builder, $value)
{
return $builder->where('name', 'LIKE', "%{$value}%");
}
}
// app/Http/Controllers/HomeController.php
namespace App\Http\Controllers;
use App\Models\User;
class HomeController extends Controller
{
public function index()
{
$users = User::applyFilter(UserFilter::class)->get()
return $users;
}
}
// app/QueryBuilders/UserQueryBuilder.php
namespace App\QueryBuilders;
use Timedoor\LaravelFilter\LaravelFilterQueryBuilder;
class UserQueryBuilder extends LaravelFilterQueryBuilder
{
}
// app/Models/User.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Timedoor\LaravelFilter\Concerns\Filterable;
use App\QueryBuilders\UserQueryBuilder;
class User extends Model
{
use Filterable;
/**
* Create a new Eloquent query builder for the model.
*
* @param \Illuminate\Database\Query\Builder $query
* @return \App\QueryBuilders\UserQueryBuilder
*/
public function newEloquentBuilder($query)
{
return new UserQueryBuilder($query);
}
/**
* @param stdClass $subject
* @param \Illuminate\Http\Request|null $request
* @param array<string, mixed> $options
* @return \App\QueryBuilders\UserQueryBuilder
*/
public static function applyFilter($subject, $options = [], Request $request = null)
{
/** @var \App\QueryBuilders\UserQueryBuilder $builder */
$builder = (new static)->newQuery();
return $builder->applyFilter($subject, $options, $request);
}
}