PHP code example of ahmmmmad11 / filters

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

    

ahmmmmad11 / filters example snippets


return [
    /*
    |--------------------------------------------------------------------------
    | Filters Path
    |--------------------------------------------------------------------------
    |
    | This value is the path where your filter class will be created.
    |
    */

    'path' => '\Http\Filters',

    /*
    |--------------------------------------------------------------------------
    | pagination rows
    |--------------------------------------------------------------------------
    |
    | The `per_page` value indicates the default pagination size. 
    | If the 'per_page' argument is not provided or there is no 
    | paginate query in the request, this value will be used.
    |
    */

    'per_page' => 100,
];

    

    namespace App\Http\Filters;
    
    use Ahmmmmad11\Filters\Filter;
    use App\Models\User;
    use Spatie\QueryBuilder\QueryBuilder;
    
    class UsersFilter extends Filter
    {
        public function filter(): Filter
        {
            $this->data = QueryBuilder::for(User::class)
                ->allowedFilters(
                    ["id","name","email","email_verified_at","created_at","updated_at"]
                );
    
            return $this;
        }
    }

...

use App\Http\Filters\UsersFilter;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(UsersFilter $filter)
    {
        return $filter->get();
    }
}

    ...
    
    public function index(UsersFilter $filter)
    {
        return $filter->paginate(30);
    }


    $filter->paginate();

    // https://example.com/users?per_page=10

// UsersController

public function index(UsersFilter $filter)
{
    return $filter->execute(function ($query) {
        $query->where('status', 'active');
    })->get();
}


//UserFilter Class

public function filter(): Filter
    {
        $this->data = QueryBuilder::for(User::class)
            ->allowedFilters(
                [...filters]
            )
            ->allowedIncludes(
                [...relations]
            );

        return $this;
    }
bash
php artisan vendor:publish --tag="filters-config"
bash
php artisan filter:make UsersFilter -r