PHP code example of amirhshokri / laravel-filterable

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

    

amirhshokri / laravel-filterable example snippets


return [
    'auto_discovery' => false,
    'namespace' => 'App\\Filterable\\Custom',
    'suffix' => 'Filter',
];

use Amirhshokri\LaravelFilterable\Main\Filterable;

class User extends Authenticatable
{
    use Filterable;
}

$users = \App\Models\User::query()
   ->filter()
   ->get();

$users = \App\Models\User::query()
   ->filter(new UserFilter())
   ->get();

use Amirhshokri\LaravelFilterable\Main\Filter\Custom\CustomFilter;

class UserFilter extends CustomFilter
{
    public function id($value, string $operator): void
    {
        //Custom filter logic here
    }
    
    public function email($value, string $operator): void
    {
        //Custom filter logic here
    }
}

$users = \App\Models\User::query()
   ->setFilterAutoDiscovery(false)
   ->filter()
   ->get();

use Amirhshokri\LaravelFilterable\Main\Filter\Custom\CustomFilter;

class UserFilter extends CustomFilter
{
    public function title($value, string $operator): void
    {
        $this->eloquentBuilder->whereHas('posts', function ($query) use ($value, $operator) {
            $query->setFilterParameters([
                    ['title', $operator, $value]
                ])->filter();
        });
    }
}

use Amirhshokri\LaravelFilterable\Main\Filterable;

class User extends Authenticatable
{
    use Filterable;
    
    protected array $allowedFilterParameters = ['id', 'email'];
}

use Amirhshokri\LaravelFilterable\Main\Filter\Custom\CustomFilter;

class UserFilter extends CustomFilter
{
    public function mobile($value, string $operator): void
    {
        $this->eloquentBuilder->where('mobile', $this->operatorMapper($operator), $value);
    }
}

use Amirhshokri\LaravelFilterable\Main\Filter\Custom\CustomFilter;
use Amirhshokri\LaravelFilterable\Main\Filter\Enum\OperatorEnum;

class UserFilter extends CustomFilter
{
    public function title($value, string $operator): void
    {
        $this->eloquentBuilder->whereHas('posts', function ($query) use ($value, $operator) {
            $query->setFilterParameters([
                    ['id', OperatorEnum::IS_NOT_EQUAL_TO, 10],
                    ['title', $operator, $value],
                    ['slug', OperatorEnum::CONTAINS, 'another pizza']
                ])->filter();
        });
    }
}
bash
php artisan vendor:publish --tag="laravel-filterable-config"