6

I have a form that includes tabs of different data. The tabs are activated by query string. After saving the content of the form that was submitted, I need to redirect back to it to show the corresponding tab using the query data. How can that be done?

currently I am using this for redirection:

return Redirect::back()->withErrors($validation);

but this returns to the first tab even there were no changes made on that tab. I saw something like

return Redirect::back()->withErrors($validation)->withQuery('tab'=>'info');

but that didn't work. Any suggestions?

4 Answers 4

6

This is probably not the best practice, but I already used it in L5.8 to redirect back with query string and it works as expected.

# Clear previous url from query string
$previousUrl = strtok(url()->previous(), '?');

return redirect()->to(
    $previousUrl . '?' . http_build_query(['foo' => 'bar'])
);

Hope this helps.

5

If you're using Redirect::to() then is simple.

return Redirect::to('route_name?q='.$append_data)

If you want to use Redirect::back() then do these following

# Pass the value while redirecting
return Redirect::back()->with('query_data', 'some_data');

And add condition to your blade file.

@if(!empty(Session::get('query_data')) && Session::get('query_data') == 'some_data')
<script>
$(function() {
    //Change the tab using javascript or
    //use location.href to refresh after appending query params to url.
});
</script>
@endif
1
  • For me, Redirect::to( needed a "\" in front of it, and even then, it abandoned the query string parameters.
    – Ryan
    Jul 7, 2018 at 1:55
2

You can use sessions for this:

return redirect()->back()->with('tab', 'info');

To get info from session, use session() global helper which works in controllers and views:

session('tab');
0

Another way is

return \Redirect::route('route_name',['query1'=>'query1value','query2','query2value']);

More Here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.