PHP code example of jti / laravelfilter

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

    

jti / laravelfilter example snippets




namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Filterable;
}


trait Filterable
{
    /**
     * @param Builder $builder
     * @param LaravelFilter $filter
     */
    public function scopeFilter(Builder $builder, LaravelFilter $filter): void
    {
        $filter->apply($builder);
    }
}


class UserFilter extends \JTI\LaravelFilter\LaravelFilter
{

    protected function initBuilder(\Illuminate\Database\Eloquent\Builder $builder): \Illuminate\Database\Eloquent\Builder
    {
        return $this->builder = $builder; // your model builder
    }

    // function name equal key name from array of params
    public function email($email = '')
    {
        if ($email) {
            $this->builder->where('email', '=', $name);
        }
    }
}
}


class BuilderController extends Controller
{
    public function index()
    {
        $filter = new UserFilter(['email' => '[email protected]']);
        $users = \App\Models\User::query()->filter($filter)->get();
        
        return view('users', compact('users'));
    }

}