How to return the page with the anchor of the LARAVEL id?

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 get back with the anchor of the id, eg:

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

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

    
asked by anonymous 21.06.2016 / 22:42

2 answers

4

If you concatenate to the previous url is what you want enough:

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

Tested on laravel 5.1 and 5.2

Version 5.3 and 5.4 we can do only:

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

Then just hit js:

var tab_on = location.hash; // #menu1
$('#ulTabs a[data-target="' +tab_on+ '"]').tab('show'); // ajustar seletor (#ulTabs...) ao seu caso
    
21.06.2016 / 23:35
0

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

/**
 * 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 url parameters before returning it, using the to() method of the same class.

Taking this into account, if we use the first line of this method, we will get something like:

  

link

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

...

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

    return redirect($url);
}

...
    
23.06.2016 / 19:19