Skip to content

Instantly share code, notes, and snippets.

@marcusedu
Last active January 22, 2023 22:07
Show Gist options
  • Save marcusedu/12e088ea8b4323beff4ded4c0df44ea0 to your computer and use it in GitHub Desktop.
Save marcusedu/12e088ea8b4323beff4ded4c0df44ea0 to your computer and use it in GitHub Desktop.
Controller base do laravel para aplicação de filtros e paginação
<?php
namespace App\Http\Controllers;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
abstract class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected function buildResponseToPaginate(Builder $builder): LengthAwarePaginator
{
$this->handleOrderBy($builder);
$this->handleWhere($builder);
$this->handleOrWhere($builder);
$this->handleWith($builder);
return $builder->paginate(perPage: $this->getPerPage($builder), columns: $this->getColumns());
}
/**
* Check if has `order by` in request and handle it.
* @param Builder $builder
* @return void
*/
protected function handleOrderBy(Builder $builder): void
{
foreach (request()->input('order_by', []) as $key => $value) {
$builder->orderBy($key, $value);
}
}
/**
* Check if has `where` in request and handle it.
* @param Builder $builder
* @return void
*/
protected function handleWhere(Builder $builder): void
{
$request = request();
if (!$request->has('where')) return;
foreach ($request->input('where') as $key => $value) {
if (is_string($value))
$builder->where($key, $value);
elseif (is_array($value)) {
if ($value[1] == 'null')
$builder->whereNull($value[0]);
elseif ($value[1] == 'not_null')
$builder->whereNotNull($value[0]);
elseif ($value[1] == 'like')
$builder->where($value[0], 'like', "%{$value[2]}%");
elseif ($value[1] == 'not_like')
$builder->where($value[0], 'not like', "%{$value[2]}%");
elseif ($value[1] == 'between')
$builder->whereBetween($value[0], $value[2]);
elseif ($value[1] == 'not_between')
$builder->whereNotBetween($value[0], $value[2]);
elseif ($value[1] == 'gt')
$builder->where($value[0], '>', $value[2]);
elseif ($value[1] == 'gte')
$builder->where($value[0], '>=', $value[2]);
elseif ($value[1] == 'lt')
$builder->where($value[0], '<', $value[2]);
elseif ($value[1] == 'lte')
$builder->where($value[0], '<=', $value[2]);
elseif ($value[1] == 'eq')
$builder->where($value[0], '=', $value[2]);
elseif ($value[1] == 'neq')
$builder->whereNot($value[0], '!=', $value[2]);
}
}
}
/**
* Check if has `or where` in request and handle it.
* @param Builder $builder
* @return void
*/
protected function handleOrWhere(Builder $builder): void
{
$request = request();
if (!$request->has('or_where')) return;
foreach ($request->input('or_where') as $key => $value) {
if (is_string($value))
$builder->orWhere($key, $value);
elseif (is_array($value)) {
if ($value[1] == 'null')
$builder->orWhereNull($value[0]);
elseif ($value[1] == 'not_null')
$builder->orWhereNotNull($value[0]);
elseif ($value[1] == 'like')
$builder->orWhere($value[0], 'like', "%{$value[2]}%");
elseif ($value[1] == 'not_like')
$builder->orWhere($value[0], 'not like', "%{$value[2]}%");
elseif ($value[1] == 'between')
$builder->orWhereBetween($value[0], $value[2]);
elseif ($value[1] == 'not_between')
$builder->orWhereNotBetween($value[0], $value[2]);
elseif ($value[1] == 'gt')
$builder->orWhere($value[0], '>', $value[2]);
elseif ($value[1] == 'gte')
$builder->orWhere($value[0], '>=', $value[2]);
elseif ($value[1] == 'lt')
$builder->orWhere($value[0], '<', $value[2]);
elseif ($value[1] == 'lte')
$builder->orWhere($value[0], '<=', $value[2]);
elseif ($value[1] == 'eq')
$builder->orWhere($value[0], '=', $value[2]);
elseif ($value[1] == 'neq')
$builder->orWhereNot($value[0], '!=', $value[2]);
}
}
}
/**
* Check if has `with` in request and handle it.
* @param Builder $builder
* @return void
*/
protected function handleWith(Builder $builder): void
{
$builder->with(request()->input('with', []));
}
/**
* Get per page from request.
* @param Builder|null $builder
* @return int
*/
protected function getPerPage(?Builder $builder = null): int
{
$request = request();
$perPage = $request->input('per_page', 15);
if ($request->has('all') && $builder != null) {
$perPage = $request->input('all') ?? $builder->count();
}
return intval($perPage);
}
/**
* Get columns from request.
* @return array
*/
protected function getColumns(): array
{
$request = request();
return $request->input('columns', ['*']);
}
protected function buildResponse(Builder $builder): Builder
{
$this->handleOrderBy($builder);
$this->handleWhere($builder);
$this->handleOrWhere($builder);
$this->handleWith($builder);
return $builder->select($this->getColumns());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment