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();
}
...
}