PHP code example of koenhoeijmakers / laravel-filterable

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

    

koenhoeijmakers / laravel-filterable example snippets


example.com/json?name=Koen&sortBy=name&desc=0


namespace App\Http\Controllers\Api\User;

use KoenHoeijmakers\LaravelFilterable\Contracts\Filtering;

final class Index
{
    public function __construct(
        private readonly Filtering $filtering
    ) {}

    public function __invoke()
    {
        $builder = User::query();
        
        $this->filtering->builder($builder)
            ->filterFor('name', fn(Builder $builder, string $value) => $builder
                ->where('name', 'like', "{$value}%");
            )
            ->sortFor('name')
            ->defaultSorting('name')
            ->filter();
    
        return UserResource::collection($builder->paginate());
    }
}