Question:
I'm trying to use Laravel 5.1 manual paging because I have a query that needs to be written with the DB select, but the pagination doesn't work! It returns me all the data on the screen without paging.
He follows:
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
$my_query = DB::select('
;WITH
UL1 As (SELECT E.ClientStoreID, MAX(E.InsertDate) AS data
FROM ClientStoreMediaExecuted AS E, ClientStore AS S
WHERE E.ClientStoreID = E.ClientStoreID
AND S.ClientID=?
GROUP BY E.ClientStoreID)
SELECT CS.* FROM UL1 RIGHT OUTER JOIN ClientStore AS CS
ON (CS.ClientStoreID = UL1.ClientStoreID)
WHERE CS.ClientID=?
AND CS.IsActive=1', [$id, $id]);
$paginator = new Paginator($my_query, count($my_query), $perPage, $currentPage, [
'path' => Request::path(),
'query' => $my_query,
'fragment' => array_slice($my_query, $perPage),
'pageName' => 'page'
]);
Answer:
Hi guys, I managed to solve it as follows:
use Illuminate\Pagination\LengthAwarePaginator; //Manually pagination
$total; //total de linhas da query
$my_query; //query
$quant = 20; //total por página
//paginação
$currentPage = Request::get('page', 1); //pega a página
$query_slice = array_slice($my_query, ($currentPage - 1) * $quant, $quant); //fatia o resultado da query
$paginacao = new LengthAwarePaginator($query_slice, $total, $quant, $currentPage); //chama a paginação
$paginacao->setPath(Request::url()); //passa a url
return $paginacao;