tekesteg
tekesteg

Reputation: 304

Laravel - Paginating Eloquent Retrieved results

This what i have in my controller

$projects=Project::where_sup_id($user_id)->get();
   $total=count($projects);
   $per_page=3;
  $projects = Paginator::make($projects, $total, $per_page);
  return View::make('project.index')->with('projects',$projects);

And this in my View

 @foreach ($projects->results as $project) 

      {{$project->title}}

 @endforeach


{{$projects->links();}}

but when i view it in the browser it is displaying all rows in all pages...the links are displaying perfectly! what do u think is the problem? please help! thank u in advance!

Upvotes: 0

Views: 4856

Answers (1)

Muhammad Usman
Muhammad Usman

Reputation: 12503

You are counting all rows, but not using limit at all, Laravel fluent query builder has skip() and take() method for that.

Btw, you don't need to manually paginate it. Laravel does pagination automatically with paginate() method.

Do it that way:

$projects = Project::where_sup_id($user_id)->paginate(3); // 3 means records per page
return View::make('project.index')->with('projects', $projects);

You are doing view work correctly.

Upvotes: 3

Related Questions