Get current URL on visit URL in Laravel

You can get current URL in laravel using url() method on request() in laravel. There are lots of method available to get the URL like url(), fullUrl(), fullUrlWithQuery(), fullUrlWithoutQuery() on request() method in Laravel.

Answers 3
  • Get URL from current URL in Laravel

    request()->url()
    
    0

    Output:

    http://localhost:8000/get-all-users

     

    The url() method returns the URL from current URL with domain but without query string.

    Here, we have web route to implement above code snippet. Just create web route and controller's method.

    Route::get('get-all-users', [HomeController::class, 'getAllUsers']);

     

  • Get full URL from current url in laravel

    request()->fullUrl()
    
    0

    Output:

    http://localhost:8000/get-all-users?page=1

    The fullUrl() method returns the full URL from the current URL with domain and query string value. You have to visit http://localhost:8000/get-all-users?page=1 to get the url with query stirng.

    Here, we have web route to implement above code snippet. Just create web route and controller's method and use code snippet in controller or view file to get the fullUrl on visit the web route in laravel.

    Route::get('get-all-users', [HomeController::class, 'getAllUsers']);

     

  • Pass query string and get full URL with query string using fullUrlWithQuery() on request() method

    request()->fullUrlWithQuery(['page' => 1])
    
    0

    Output:

    http://localhost:8000/get-all-users?page=1

    This code snippet will help you to pass the query string (key/value) and get the URL with query string in laravel.

    It returns the url (domain with query string values). You can use above code snippet in web.php, controller files and also in views file in laravel.

    Here, we have web route to implement above code snippet.

    Route::get('get-all-users', [HomeController::class, 'getAllUsers']);

     

  • Back to code snippet queries related laravel