PHP code example of acodingproject / laravel-filterable

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

    

acodingproject / laravel-filterable example snippets


public function recent($minutes = null)
{
    $minutes = (is_numeric($minutes)) ? $minutes : 30;

    return $this->builder->where('created_at', '>=', Carbon\Carbon::now()->subMinutes($minutes));
}


namespace App\Filters;

use Kyslik\LaravelFilterable\Filterable;

class UserFilters extends Filterable
{
    public function filterMap()
    {
        return ['recent' => ['recently', 'recent']];
    }
	
    public function recent($minutes = null)
    {
        $minutes = (is_numeric($minutes)) ? $minutes : 30;

        return $this->builder->where('created_at', '>=', \Carbon\Carbon::now()->subMinutes($minutes)->toDateTimeString());
    }
}

use Kyslik\LaravelFilterable\FilterableTrait;

...
class User extends Model 
{
    use FilterableTrait;
    ...
}

public function index(User $user, UserFilters $filters) 
{
    return $user->filter($filters)->paginate();
}


namespace App\Filters;

use Kyslik\LaravelFilterable\GenericFilterable;

class UserFilters extends GenericFilterable
{
    protected $filterables = ['id', 'username', 'email', 'created_at', 'updated_at'];
}

use Kyslik\LaravelFilterable\FilterableTrait;

...
class User extends Model 
{
    use FilterableTrait;
    ...
}

public function index(User $user, UserFilters $filters) 
{
	return $user->filter($filters)->paginate();
}

...
class UserFilters extends GenericFilterable
{
    protected $filterables = ['id', 'username', 'email', 'created_at', 'updated_at'];
    
    protected function settings()
    {
        // global settings for this filter, pick either "except" or "only" logic
        $this->only(['=', '~', '!~']);
        // $this->except(['!=']);

        // settings applied only to some columns, these settings ignore settings above
        $this->for(['username', 'id'])->only(['!=', '>=', '=', '~']);
        $this->for(['id'])->only(['=', '!=', '~']); //settings for "id" will be re-written
    }
}

/?filter-created_at=t>=1510952444
/?filter-id=><1,19
/?filter-id=i=1,5,10,12
/?filter-admin=b=yes
/?filter-username=joe
/?filter-username=~joe
/?filter-username=~joe&filter-admin=b=yes&filter-created_at=t=1510952444