PHP code example of abdallahmohammed / laravel-eloquent-filter

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

    

abdallahmohammed / laravel-eloquent-filter example snippets


LaravelEloquentFilter\Providers\LaravelServiceProvider::class,

'namespace' => "App\\Http\\Filters\\",

$app->register(LaravelEloquentFilter\Providers\LumenServiceProvider::class);

config(['laravel-eloquent-filter.namespace' => "App\\Http\\Filters\\"]);

 

namespace App\Http\Filters;

use LaravelEloquentFilter\BaseFilter;

class UserFilter extends BaseFilter
{
    /**
     * Registered filters to operate upon.
     *
     * @var array
     */
    protected $filters = [
        //
    ];
}

protected $filters = [
    'first_name',
    'email',
];

/**
 * @return \Illuminate\Eloquent\Builder
 */
public function firstName($value) {
    return $this->builder->where('first_name', $value);
}

/**
 * @return \Illuminate\Eloquent\Builder
 */
public function name($value) {
    return $this->builder->where('name', 'like', "%$value%");
}

public function defaultName($value)



namespace App;

use LaravelEloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use Filterable;

    ...
}

namespace App\Http\Controllers;

use App\Models\User;
use App\Http\Filters\UserFilter;

class UserController extends Controller
{
    public function index(UserFilter $filter)
    {
        return User::filter($filter)->get();
    }

    ...
}

namespace App\Http\Controllers;

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

class UserController extends Controller
{
    public function index(Request $request)
    {
        return User::filter(new UserFilter($request->query()))->get();
    }

    ...
}
bash
php artisan vendor:publish --provider="LaravelEloquentFilter\Providers\LaravelServiceProvider"