How to Get Base URL in 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, laravel 9, laravel 10 and laravel 11 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Redirect to Route from Controller Example
- Laravel Ajax DELETE Request Example Tutorial
- Laravel Ajax GET Request Example Tutorial
- Laravel 9 Ajax Request Example Tutorial
- Laravel React JS Axios Post Request Example Tutorial
- How to Check Request is Ajax or Not in Laravel?
- Laravel Get Route Parameters in Middleware Example
- How to Check Request Method is GET or POST in Laravel?
- How to Check User Login or Not in Laravel?
- How to Make Custom Middleware in Laravel?
- How to Get Current Route Name in Laravel?
- How to Redirect Route with Querystring in Laravel?