Manual Paging in Laravel 5.1

3

I'm trying to use Laravel 5.1 manual paging because I have a query that needs to be written with DB's select, but paging does not work! It returns me all the data on the screen without paging.

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'
]);
    
asked by anonymous 06.08.2015 / 16:40

1 answer

1

Hi everyone, I have been able to solve this:

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;
    
03.02.2016 / 21:38