Get Current URL with Parameters in Laravel 6, 7 & 8
To get the current URL with parameters in Laravel 6, 7 & 8 use the fullUrl()
method from the Request()
class.
Here is an example of how you would do it in your controller file:
$current_url = \Request::fullUrl();
dd($current_url);
In a view blade, you would get the current URL like this:
{{ \Request::fullUrl() }}
Get Without URL without Parameters in Laravel
If you don't need parameters in the current URL, use the url()
method from the Request()
class like this:
$current_url = \Request::url();
dd($current_url);
And in a view blade:
{{ \Request::url() }}
Get a Segment from Current URL in Laravel
If you need to only get a segment from the current URL in Laravel, use the segment()
method from the Request()
class. Pass the number of the segment to get as the first argument.
$param = \Request::segment(2);
dd($param);
And in a view blade:
{{ \Request::segment(2) }}
Note – if the URL segment doesn't exist, segment()
will throw an error, so you'll need to create some logic to handle this unless you're confident the segment you are accessing will always be there.