Support the ongoing development of Laravel.io →
posted 10 years ago
Input Views
Last updated 1 year ago.
0

This is my current solution, and it's really ugly

public function show($category_id, $subject_id)
	{
		$items = 15;
		if(!Input::get('page')) {
			$posts = Post::where('subject_id',$subject_id)->paginate($items);
			Paginator::setCurrentPage($posts->getLastPage());
		}
		$subject = Subject::find($subject_id);
		$posts = Post::where('subject_id',$subject_id)->paginate($items);
		$category = Category::find($category_id);

		return View::make('subjects.show')
			->with(compact('subject'))
			->with(compact('posts'))
			->with(compact('category'));
	}
Last updated 1 year ago.
0

Solution seems pretty much fine to me --- but why do you need to call the identical query twice on posts? Unnecessary. Simply place that line after $items = 15, then delete it in the two current positions, and behavior should be identical.

Last updated 1 year ago.
0
Last updated 1 year ago.
0

@andrewsuzuki

You need to call Paginator::setCurrentPage() before any pagination.

That's why He does it like this I guess.

Last updated 1 year ago.
0

Why not just sort in the different direction, so the last entry is displayed on the first page?

Post::where('subject_id',$subject_id)->orderBy('created_at','desc')->paginate(10);
Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Hambern hambern Joined 22 Dec 2013

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.