Persistir filtro no paginate
[Finalizado Pelo Aluno]
Criador
Felipe Reis
Olá Carlos!
tudo bom?
E como seria para persistir os filtros no paginate?
Manager
Carlos Ferreira
Olá, Felipe!
Como vai?
É bem fácil persistir, basta passar os dados do filtro na paginação, com o método appends, veja o exemplo: https://github.com/especializati/larafood/blob/master/resources/views/admin/pages/products/index.blade.php#L51
Criador
Felipe Reis
Olá Carlos tudo bom?
desculpa formulei mal a pergunta.
Como eu persistiria o filtro com ajax?
Manager
Carlos Ferreira
Está usando qual tecnologia no frontend? Porque basta passar via query params, ex: ?page=2&filter1=bla&filter2=blo
Criador
Felipe Reis
Estou usando vue.js. ah legal entendi, muito obrigado!
Deividson Venancio Magalhaes
Isso resolve na api
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
class Product extends Model
{
use HasFactory;
private int $totalPage = 30;
protected $fillable = ['name', 'description'];
public function search():LengthAwarePaginator
{
$filters = $this->getFilters();
$query = $this->applyFilters($filters);
return $this->applyPagination($query, $filters);
}
public function getFilters(): array
{
return[
'name'=> request()->query('name', null),
'description' => request()->query('description', null)
];
}
public function applyFilters(array $filters):Builder
{
return self::where(function ($query) use ($filters){
if($filters['name']) $query->where('name', 'LIKE', "%{$filters['name']}%");
if($filters['description']) $query->orWhere('description', 'LIKE', "%{$filters['description']}%");
return $query;
});
}
public function applyPagination(Builder $query, $filters):LengthAwarePaginator
{
$paginator = $query->paginate($this->totalPage);
if($filters['name']) $paginator->appends(['name' => $filters['name']]);
if($filters['description']) $paginator->appends(['description' => $filters['description']]);
return $paginator ;
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
class Product extends Model
{
use HasFactory;
private int $totalPage = 30;
protected $fillable = ['name', 'description'];
public function search():LengthAwarePaginator
{
$filters = $this->getFilters();
$query = $this->applyFilters($filters);
return $this->applyPagination($query, $filters);
}
public function getFilters(): array
{
return[
'name'=> request()->query('name', null),
'description' => request()->query('description', null)
];
}
public function applyFilters(array $filters):Builder
{
return self::where(function ($query) use ($filters){
if($filters['name']) $query->where('name', 'LIKE', "%{$filters['name']}%");
if($filters['description']) $query->orWhere('description', 'LIKE', "%{$filters['description']}%");
return $query;
});
}
public function applyPagination(Builder $query, $filters):LengthAwarePaginator
{
$paginator = $query->paginate($this->totalPage);
if($filters['name']) $paginator->appends(['name' => $filters['name']]);
if($filters['description']) $paginator->appends(['description' => $filters['description']]);
return $paginator ;
}
}
Precisa estar logado para conseguir responder a este ticket!
Clique Aqui Para Entrar!