How to Get Base URL in Laravel?

By Hardik Savani October 6, 2022 Category : Laravel

Hey,

In this comprehensive tutorial, we will learn how to get site url in laravel. you will learn laravel get base url in blade. you can understand a concept of laravel get base url in controller. This article will give you a simple example of laravel get base url in view. Alright, let us dive into the details.

You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 versions.

In this example, i will give you simple two examples to getting base url in blade and controller file using laravel URL facade. so, let's see the simple example code:

Example 1: Laravel Get Base URL in Controller File

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use URL;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

/* Get Base URL using URL Facade */

$url = URL::to("/");

/* Get Base URL using url() helper */

$url2 = url('/');

dd($url, url2);

}

}

Output:

https://yourdomain.com

https://yourdomain.com

Example 2: Laravel Get Base URL in Blade File

{{ URL::to("/") }}

{{ url('/') }}

Output:

https://yourdomain.com

https://yourdomain.com

I hope it can help you...

Tags :