Home » Laravel Global Variables Example Tutorial From Scratch

Laravel Global Variables Example Tutorial From Scratch

Today, We want to share with you Laravel Global Variables Example Tutorial From Scratch.In this post we will show you set global view variables using composer share in Laravel – Superglobals, hear for Laravel 5.6 – Sharing $user variable in all views we will give you demo and example for implement.In this post, we will learn about Laravel 5 global variable in all views file with an example.

Laravel Global Variables Example Tutorial From Scratch

There are the Following The simple About Laravel Global Variables Example Tutorial From Scratch Full Information With Example and source code.

Read Also:  Laravel Eloquent Queries Joins Example

As I will cover this Post with live Working example to develop How to set global view variables using composer or view share in Laravel, so the some laravel pass data to view from controller for this example is following below.

  • using AppServiceProvider
  • using Composer

Thetre for, In this Laravel simple example we will share varible from Laravel file “AppServiceProvider.php”, Therefor let’s display below both main example and use it.

Using View Share

app/Providers/AppServiceProvider.php

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     * Laravel - How to set and use global variables?
     * @return void
     */
    public function boot()
    {
        view()->share('totalProduct', '28 - pakainfo.com');
       
    }
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Laravel global variable in all views file

Using Composer

app/Providers/AppServiceProvider.php

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     * laravel 5 set global variable controller
     * @return void
     */
    public function boot()
    {
       	view()->composer('*', function ($view) {
            $view->with('totalProduct', 'pakainfo.com');
        });
    }
    /**
     * Register any application services.
     * laravel global constants
     * @return void
     */
    public function register()
    {
        //
    }
}

And Last you can simple display or get access $totalProduct share varible like as bellow simple used :

Read Also:  Rendering Lists in VueJs Example

How to use:


{{ $totalProduct }}

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel Global Variables Example Tutorial From Scratch.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  Laravel set global Variable in middleware, all controller and views

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  How to Declare Global Variable in Laravel 6?
  2. Read Also:  Handle CORS Requests in Laravel 6 Application
  3. Read Also:  Shorthand comparisons in Laravel Ternary Operators (?:)
  4. Read Also:  Receive JSON POST Data using PHP
  5. Read Also:  Global variable in laravel controller Example
  6. Read Also:  Laravel 7 Email Verification Tutorial and Example
  7. Read Also:  Simple VueJS date picker Component
  8. Read Also:  laravel 5.8 global constants variable
  9. Read Also:  Removing index.php From Codeigniter URL htaccess
  10. Read Also:  Multiple file Upload in Laravel 5.7 with validation

Leave a Comment

Your email address will not be published. Required fields are marked *