1. Go to this page and download the library: Download holoyan/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/ */
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
public function index(Request $request)
{
return User::filter($request->all())->get();
}
}
namespace App\Models;
// import Filterable trait
use holoyan\EloquentFilter\Filterable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Filterable;
// other stuff here
}
namespace App\Http\Filters;
use holoyan\EloquentFilter\Filter;
use holoyan\EloquentFilter\Rules\SimpleRule;
class UserFilter extends Filter
{
public function rules()
{
return [
'email' => SimpleRule::make()->startsWith(),
'categories' => SimpleRule::make(),
'role' => SimpleRule::make(),
];
}
}
namespace App\Models;
use holoyan\EloquentFilter\Filterable;
use Illuminate\Foundation\Auth\User as Authenticatable;
// your custom filter class
use App\Filters\MyFilter;
class User extends Authenticatable
{
use Filterable;
public static $filterClass = MyCustomFilter::class;
// other stuff here
}
public function rules()
{
return [
'firstName' => SimpleRule::make()->setColumn('first_name'),
];
}
public function rules()
{
return [
'firstName' => SimpleRule::make()->setvalue(function($value){
return $value . 'test';
} ),
];
}
public function rules()
{
return [
'name' => SimpleRule::make()
];
}
return [
// where name="value"
'name' => SimpleRule::make(),
// where name like "value%"
'name' => SimpleRule::make()->startsWith(),
// where name like "%value"
'name' => SimpleRule::make()->endsWith(),
// where name like "%value%"
'name' => SimpleRule::make()->contains()
];