Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I redirect back with query string?

Tags:

php

laravel

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?

like image 808
Skeletor Avatar asked Oct 08 '16 12:10

Skeletor


People also ask

How do I run a query string?

A Query String Collection is used to retrieve the variable values in the HTTP query string. If we want to transfer a large amount of data then we can't use the Request. QueryString. Query Strings are also generated by form submission or can be used by a user typing a query into the address bar of the browsers.

How would you redirect back to a controllers action?

Redirect to a Controller Action The last piece of the puzzle is that you can redirect to a specific Method of a specific Controller, no matter what its Route or URL is – use method action(): return redirect()->action('App\Http\Controllers\BooksController@index');


1 Answers

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
like image 134
imrealashu Avatar answered Oct 12 '22 11:10

imrealashu