Today, We want to share with you Laravel Global variable Sharing Data with all views.In this post we will show you Laravel 5.7 – Sharing $user variable in all views, hear for How to share Variable with all Views in Laravel we will give you demo and example for implement.In this post, we will learn about Sharing Data Between Views Using Laravel View Composers with an example.
Laravel Global variable Sharing Data with all views
There are the Following The simple About Laravel Global variable Sharing Data with all views Full Information With Example and source code.
As I will cover this Post with live Working example to develop Sharing Data With All Views, so the How to share Variable with all Views in Laravel for this example is following below.
- view share laravel 5
- laravel global variable
- laravel view share multiple
- sharing data with all views laravel
Step 1 : Laravel View Files
resources/views/home.blade.php
@extends('layouts.layout') @section('content') <h3>sharing data with all views laravel</h3> <div class="row"> <div class="col-md-12"> <div class="panel panel-default"> <div class="panel-heading">Dashboard</div> <div class="panel-body"> {{ Auth::team()->name }}, You are logged in! {{$currentmembers}} </div> </div> </div> </div> @endsection
Step 2 : Include Providers
app/Providers/AppServiceProvider.php
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use DB; use \Illuminate\Support\Facades\View; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { $members = DB::table('members')->get()->count(); View::share('currentmembers', $members); } /** * Register any application services. * Laravel Global variable Sharing Data with all views * @return void */ public function register() { // } }
Laravel 5.6 – Sharing variable in all views
Step 1: Create Route
routes/web.php
Route::get('home', function () { return view('home'); }); Route::get('team', function () { return view('team'); });
Step 2 : Create Blade View
resources/views/home.blade.php
<html> <body> <h1>Home Page</h1> <p><?= $teamname ?></p> </body> </html>
resources/views/team.blade.php
<html> <body> <h1>Team Page</h1> <p><?= $teamname ?></p> </body> </html>
Step 3: Share Variables
app/Providers/AppServiceProvider.php
# app/Providers/AppServiceProvider.php public function boot() { // add Your get varibles data this view()->share('teamname', 'Latest 2021 Players Teams'); }
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 variable Sharing Data with all views.
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.