Laravel Global Variable for All View (Blade) Files Tutorial

Share this Article
Reading Time: 3 minutes
442 Views

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.

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

Learn More –

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

URL: http://127.0.0.1:8000

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.

Add your shareable global variables by boot() method.

<?php

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>
<html>
<head>
    <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>
</head>
<body>
  
    <h1>{{ $projectTitle }}</h1>
    <h2>{{ $defaultMessage }}</h2>
  
</body>
</html>
  

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

Buy Me a Coffee

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.

Was this post helpful?

PHP Web Development Courses on Offer in just $4
PHP Web Development Courses on Offer in just $4