PHP code example of ans-group / laravel-sieve

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

    

ans-group / laravel-sieve example snippets




class Pet extends Model implements Searchable
{
    public function sieve(Sieve $sieve)
    {
        $sieve->configure(fn ($filter) => [
           'name' => $filter->string(),
           'breed' => $filter->enum(['Beagle', 'Tiger']),
        ]);
    }
}



class PetController extends Controller
{
    public function index()
    {
        return Pets::query()->search()->paginate();
    }
}



class Pet extends Model implements Searchable
{
    public function sieve(Sieve $sieve)
    {
        $sieve->configure(fn ($filter) => [
             'name' => $filter->string(),
             'breed' => $filter->enum(['Beagle', 'Tiger']),
          ],
          ['name']       
        );
    }
}

$sieve->setDefaultSort('name', 'asc')


$filter->string()


$filter->numeric()


$filter->enum(['HR', 'RnD'])


$filter->boolean() // defaults to 1 and 0
$filter->boolean('Yes', 'No') // search for Yes and No in the database


$filter->date(),


$sieve->configure(fn (FilterBuilder $filter) [
    'name' => $filter->string(),
]);


$sieve->configure(fn ($filter) => [
    'owner_name' => $filter->for('owner.name')->string()
])


$sieve->configure(fn ($filter) => [
    'date_created' => $filter->for('created_at')->date()
])



class PenceFilter implements WrapsFilter
{
    protected $filter;

    public function setFilter($filter)
    {
        $this->filter = $filter;
    }

    public function modifyQuery($query, SearchTerm $search)
    {
        $pence = $search->term() * 100;
         // will do a better API for making new search terms later
        $newTerm = new SearchTerm($search->property(), $search->operator(), $search->column(), $pence);

        $this->filter->modifyQuery($query, $search);
    }

    public function operators()
    {
        return $this->filter->operators();
    }
}

$sieve->configure(fn ($filter) => [
    'price' => $filter->wrap(new PenceFilter)->numeric(),
])