PHP code example of laraditz / model-filter

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

    

laraditz / model-filter example snippets


use Laraditz\ModelFilter\Filterable;

class User extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Filterable;
    ...
}

namespace App\Filters;

use Laraditz\ModelFilter\Filter;
use Illuminate\Database\Eloquent\Builder;

class UserFilter extends Filter
{
    public function name(string $value)
    {
        $this->where('name', 'LIKE', $value);
    }

    public function email(string $value)
    {
        $this->where('email', 'LIKE', "%$value%");
    }

    // Filter relationship
    public function rank($value)
    {
        $this->whereHas('rank', function (Builder $query) use ($value) {
            $query->where('level', 'like', $value);
        });
    }
}



protected $filterable = [
    'name', 'email'
];

$users = User::filter($request->all())->get();

/users?name=farhan&rank=novice

/users?name=farhan&rank=novice&sort=name,level