How to Get the Current URL in Laravel
Learn how to easily get the current URL in Laravel using simple code snippets and the Request facade. Get started today with our beginner-friendly guide
In Laravel, you can get the current URL using the url()
helper function or the Request
facade. Here are the two methods to get the current URL in Laravel:
Method 1: Using the url()
helper function
$currentUrl = url()->current();
This will return the current URL, including any query parameters.
Method 2: Using the Request
facade
First, you need to add the following line at the top of your PHP file to import the Request
facade:
use Illuminate\Http\Request;
Then, you can get the current URL using the url()
method of the Request
object:
$currentUrl = Request::url();
This will return the current URL, including any query parameters.
Both of these methods will return the current URL as a string, which you can then use as needed in your application.