How to return the page with the "Chor" of the LARAVEL id?

Asked

Viewed 1,094 times

5

After submitting the form I return to the page in question with two methods, redirect() and back(). In case I’m using the bootstrap tabs and would like to return with the id anchor, ex:

return redirect()->back();  // #menu1 ??

Note* passing the method with() it does not identa in the url

  • You can include the bootstrap code sff.. At least tabs to see the classes. Is this what you mean? http://jsfiddle.net/xfw8t/12/

  • Yes, I’d like to return to the corresponding tab

  • Done on @Igor, I think this is the best for what you want to do

2 answers

4

Concatenating to the previous url is enough :

$urlBack = redirect()->back()->getTargetUrl();
return redirect($urlBack. '#menu1');

Tested in Laravel 5.1 and 5.2

Version 5.3 and 5.4 we can do only:

return redirect(url()->previous(). '#menu1');

Then enough about js:

var tab_on = location.hash; // #menu1
$('#ulTabs a[data-target="' +tab_on+ '"]').tab('show'); // ajustar seletor (#ulTabs...) ao seu caso
  • Thank you, thank you Miguel!

0

When you use the method back(), it makes use of the class Urlgenerator, to be more specific, the method previous():

/**
 * Get the URL for the previous request.
 *
 * @return string
 */
public function previous()
{
    $referrer = $this->request->headers->get('referer');

    $url = $referrer ? $this->to($referrer) : $this->getPreviousUrlFromSession();

    return $url ?: $this->to('/');
}

This method validates and removes the parameters from the url before returning it through the method to() of that same class.

Taking that into account, if we use the first line of this method, we will receive for example, something like:

http://localhost:8000/user? id=1&name=Rafael

If you combine this with the method redirect(), you will return to the same previous url, including its parameters. For example:

...

public function index()
{
    // the previous url with the old params.
    $url = request()->headers->get('referer');

    return redirect($url);
}

...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.