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 5.8 global constants variable

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:  PHP Contact Form Send Email File Attachment 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:  Global variable in laravel controller Example

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel set global Variable in middleware, all controller and views
  2. Read Also:  Access Control Allow Origin http set Headers
  3. Read Also:  Remove Last Character from String in JavaScript
  4. Read Also:  PHP Get User IP Address
  5. Read Also:  How to Declare Global Variable in Laravel 6?
  6. Read Also:  How to declare a global variable in Laravel 5.8?
  7. Read Also:  Curl GET And POST Method Calls – PHP Example
  8. Read Also:  Ajax Live Data Search using Jquery PHP MySql
  9. Read Also:  How to get the browser viewport height and width dimensions?
  10. Read Also:  jQuery get value of selected radio button
CLOSEX