PHP code example of thecodemill / eloquent-filter

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

    

thecodemill / eloquent-filter example snippets


// With query modifiers
$users = User::where('email', $request->input('email'))
    ->where('age', $request->input('age'))
    ->whereHas('subscription', function ($query) use ($request) {
        $query->where('code', $request->input('subscription'));
    })
    ->get();

// Using EloquentFilter's filter handlers
$users = User::filter($request->all())->get();

namespace App;

use TheCodeMill\EloquentFilter\Filterable;

class User
{
    use Filterable;
    
    /**
     * Return the filter handlers.
     *
     * @return array
     */
    public static function filters()
    {
        return [
            'email' => function ($query, $value) {
                return $query->where('email', $value);
            },
            'age' => function ($query, $value) {
                return $query->where('age', $value);
            },
            'subscription' => function ($query, $value) {
                return $query->whereHas('subscription', function ($query) use ($value) {
                    $query->where('code', $value);
                });
            }
        ];
    }
}

// Paginate models in your controller/repository/etc.
$users = User::filter($request->all())->paginate();

// In Blade, append only the valid query parameters:
{{ $users->appends(App\User::validFilters(request()->all()))->links() }}