PHP code example of kolirt / laravel-model-filter

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

    

kolirt / laravel-model-filter example snippets




namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Kolirt\ModelFilter\Filterable;

class User extends Authenticatable
{
    use Notifiable, Filterable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}




namespace App\Models\Filters;

use Illuminate\Database\Eloquent\Builder;

class UserFilter
{

    public function q(Builder $query, $value)
    {
        $query->where('name', 'LIKE', '%' . $value . '%');
    }

}



namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class TestController extends Controller
{

    public function index(Request $request)
    {
        User::filter([
            'q' => 'q'
        ])->get();

        // equal

        User::where('name', 'LIKE', '%q%')->get();
    }

}