Manual Paging in Laravel 5.1

Asked

Viewed 1,005 times

3

I am trying to use the manual paging of Laravel 5.1 because I have a query that needs to be written with the DB select, but the 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'
]);
  • You cannot use the function paginate() of Laravel ?

  • I can’t use it. Laravel’s paginate() function only works when the query has no group by and also does not work with DB::select only with DB::table and Eloquent. I will edit the question and put the query.

  • 1

    What is the value of the variable $perPage ?

  • the value is 20 per page.

1 answer

1


Hi guys I managed to resolve 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.