Udipta Gogoi
Udipta Gogoi

Reputation: 101

Laravel redirecting to the previous page

I am inserting data to the database and redirecting back to the previous page using the code below in laravel

public function store(Request $request)
{

    $todo=new Todo;
    $todo->todo=$request->todo;
    $todo->save();
    return redirect()->back();
}

See My web page enter image description here

After entering some data "new todo" it works well; data inserted and shows in the page but the page becomes like below.
I think the stylings are not working in the page

See the image

And after refreshing the page i got the page well.
What should be the problem?

Upvotes: 0

Views: 1454

Answers (2)

Chirag Patel
Chirag Patel

Reputation: 1642

You can save the previous URL in session so that you can easily redirect back to their previous URL,

Try this in your function,

public function store(Request $request)
{
    $request->session()->put('url.intended',url()->previous());
    $todo=new Todo;
    $todo->todo=$request->todo;
    $todo->save();
    return redirect($request->session()->get('url.intended'));
}

Here, the Previous URL is generated by Helpers function of laravel.

Please comment if you have any doubts.

Upvotes: 0

Rahmad Al Habib
Rahmad Al Habib

Reputation: 330

You can try this

return back()->withInput();

Upvotes: 1

Related Questions