PHP code example of mammothcoding / laravel-easy-filter

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

    

mammothcoding / laravel-easy-filter example snippets


use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Mammothcoding\LaravelEasyFilter\EasyFilter;

Route::get('/users', static function (Request $request) {
    $filter = new EasyFilter('App\Models\User', $request); // Creating a filter object, specifying the model, passing the request data
    $filter->filter(); // Applying a filter
    $filter->sort(); // Sorting
    $result = $filter->getResultArray(); // Get the result in the array
    return view('users', ['res' => $result]); // Controller returns the rendered view with result
});

namespace App\Http\Controllers;

use App\User;
use Mammothcoding\LaravelEasyFilter\EasyFilter;

class UsersController
{
    public function index()
    {
        $filter = new EasyFilter('App\Models\User', request()); // Creating a filter object, specifying the model, passing the request data
        $result = $filter->filter()->toArray(); // Apply the filter and convert the resulting collection into an array

        return view('users', ['$result' => $result]); // Controller returns the rendered view with result
    }
}

namespace App\Http\Controllers;

use App\User;
use Mammothcoding\LaravelEasyFilter\EasyFilter;

class UsersController
{
    public function index()
    {
        $filter = new EasyFilter('App\Models\User', request()); // Creating a filter object, specifying the model, passing the request data
        $filter->filter(); // Applying a filter
        $filter->sort(); // Sorting
        $result = $filter->getResultBuilder()
                ->paginate($this->request->input('perpage') ?? 1000)
                ->toArray();

        return view('users', ['$result' => $result]); // Controller returns the rendered view with result
    }
}

namespace App\Http\Controllers;

use App\User;
use Mammothcoding\LaravelEasyFilter\EasyFilter;

class UsersController
{
    public function index()
    {
        $filter = new EasyFilter('App\Models\User', request()); // Creating a filter object, specifying the model, passing the request data
        $filter->filter(); // Applying a filter
        $filter->sort(); // Sorting
        $result = $filter->getResultArray(); // Get the result as an array

        return view('users', ['$result' => $result]); // Controller returns the rendered view with result
    }
}