Q:

Get URL segment in Laravel 5

For accessing previous URL in laravel. I am using this code in my controller.
$current_url = Request::url();
$back_url = redirect()->back()->getTargetUrl();
if ($current_url != $back_url) {
  Session::put('previous_url', redirect()->back()->getTargetUrl());
}

This method helps maintainging previous url even when server side validation fails.
In my blade I access previous url like this {{ Session::get('previous_url') }}.
I need to find the second segment of my previous url.
Thanks

A:

You can do it this way:
request()->segment(2);

request() is a helper function that returns Illuminate\Http\Request, but you can also use the facade Request or inject the class as a dependency in your method.
EDIT
with the redirect back: redirect()->back()->getRequest()->segment(2);

