2016-10-11 4 views
1

Ich versuche, eine Paginierung manuell in Laravel zu schaffen, aber seine alle Elemente in meinem Modell anstelle des ‚perpage‘ Wert zurückgibt, die ich gesetzt, unten ist mein Code:Laravel LengthAwarePaginator Klasse nicht funktioniert

$a = mazee\ad::all(); 
    $p = new \Illuminate\Pagination\LengthAwarePaginator($a ,count($a),3, 1); 

    dd($p) ; 

auch alle Links: '? page = 2', 'page = 3?', 'page = 4?' sind outputing das gleiche Ergebnis enter image description here

Antwort

4

Bitte beachten Sie das Nutzungs Beispiel \Illuminate\Pagination\LengthAwarePaginator in der Klasse \Illuminate\Database\Query\Builder:

/** 
    * Paginate the given query into a simple paginator. 
    * 
    * @param int $perPage 
    * @param array $columns 
    * @param string $pageName 
    * @param int|null $page 
    * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator 
    */ 
    public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null) 
    { 
     $page = $page ?: Paginator::resolveCurrentPage($pageName); 

     $total = $this->getCountForPagination($columns); 

     $results = $total ? $this->forPage($page, $perPage)->get($columns) : []; 

     return new LengthAwarePaginator($results, $total, $perPage, $page, [ 
      'path' => Paginator::resolveCurrentPath(), 
      'pageName' => $pageName, 
     ]); 
    } 

Der erste an LengthAwarePaginator ($ results) übergebene Parameter sollte nicht alle Zeilen Ihres Modells sein. Hoffe, es hilft: D

1

schließlich ich es wie so gelöst:

class maincontroller extends Controller 
{ 
     public function getprofile(){ 
      $a = mazee\ad::all()->toarray(); 

      $this->paginate($a , 5)->items(); 

     } 



      public function paginate($items , $perpage){ 
      $total = count($items); 
      $currentpage = \Request::get('page', 1); 
      $offset = ($currentpage * $perpage) - $perpage ; 
      $itemstoshow = array_slice($items , $offset , $perpage); 
      $p = new LengthAwarePaginator($itemstoshow ,$total ,$perpage); 
      $p->setPath('http://localhost/agroexpresslink.com/profile'); 
      return $p; 
     } 
} 
Verwandte Themen