morawcik's avatar

Redirect to created object on paginated page

Hello I have forum with post pagination. After create new one how I can redirect to it? Redirect back or sending page number by hidden input (or something like this) can do that but what about situation if new post will be on new page (before create pagination return 2 pages but after return 3) ?

0 likes
3 replies
RachidLaasri's avatar

Redirecting to the new created object is simple, You can do it with

    $post = new Post();
    $post->title = Input::get('title');
    $post->slug =  Input::get('slug');
    $post->content = Input::get('content');
    $post->save();
    return Redirect::route('posts.show', ['id' => $post->id]);

OR :

     $post = Post::create(Input::all());
     return Redirect::route('posts.show', ['id' => $post->id]);

Both will work.

But i didn't understand the second question.

morawcik's avatar

I want redirect to paginated page with actual created post. Not to page with this one post but to page with list. Oh maybe this way: I want redirect to last paginated page. If Post::paginate(10) (after create new post) return 3 pages that I want redirect to 3th page.

EDIT: Oh I get it: getLastPage method :)

RachidLaasri's avatar
Level 41

Oh, yeah .. now i understand what you wanna do.

Yes, you can tho that in your store method. Grab all posts & paginate them and redirect to getLastPage.

Please or to participate in this conversation.