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.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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.

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 :

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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   How to add custom jquery image validation?
  2.   Global variable in laravel controller Example
  3.   Laravel JSON Array Merge into one array
  4.   How to Declare Global Variable in Laravel 6?
  5.   Laravel set global Variable in middleware, all controller and views
  6.   How to add bootstrap accordion with plus minus Toggle Icons?
  7.   Laravel 6.2 jQuery AJAX CRUD Tutorial
  8.   laravel 5.8 global constants variable
  9.   How to Clear cache in Laravel 5.8?
  10.   How to declare a global variable in Laravel 5.8?

Leave a Reply

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