Laravel Global Variable for All View (Blade) Files Tutorial

Reading Time: 3 minutes

Inside this tutorial we will see the concept i.e How To Create Laravel Global Variable for all View Files. Also we can say How to Create default variables with their values in laravel. There is a very simple step by which we can register the global variables for application.

If you are looking for a solution to add global variables in laravel which you can use globally to every view files then this article will help you a lot for this.

Read More: Laravel How to Check If Blade View File Exists Tutorial

This article is not specific to any version of laravel. You can use this concept to any version of it.

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve


Assuming laravel already installed inside your system.

Register Global Variable For Views

To register a global variable which you can access inside anywhere inside blade templates, Open AppServiceProvider.php file from /app/Providers folder.

Read More: Remove HTML Tags From String Tutorial in CodeIgniter 4

Add your shareable global variables by boot() method.


namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Models\Setting;

class AppServiceProvider extends ServiceProvider
     * Register any application services.
     * @return void
    public function register()

     * Bootstrap any application services.
     * @return void
    public function boot()
        // with Static value
        view()->share("defaultMessage", "This is a default message");

        // with DB value
        view()->share('projectTitle', Setting::where('name', 'project_title')->value('value'));

How To Use Shared Global Variables To Views

As simple as that, you need to use in same way as normal variables uses.

Let’s consider a view file i.e demo.blade.php

<!DOCTYPE html>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel Global Variable for All View (Blade) Files Tutorial</title>
    <h1>{{ $projectTitle }}</h1>
    <h2>{{ $defaultMessage }}</h2>

We hope this article helped you to learn Laravel Global Variable for All View (Blade) Files Tutorial in a very detailed way.

Read More: Laravel How To Remove HTML Tags From String Tutorial

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more