Laravel Global Variables Example Tutorial From Scratch

Laravel Redirect To Another URL / Web Page - HTTP Laravel Redirects

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:  github login command line

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.

Read Also:  paypal payment Gateway Integration with laravel 5.5 -Paypal checkout Laravel

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:  How to Declare Global Variable in Laravel 6?

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.

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

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