Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks

Sharing variables between all views in laravel 8

Aman Jain, January 15, 2022January 15, 2022

In laravel its easy to share varible from controller to view using view function but sometimes we need to share some specific information to all pages of our website for examle sharing the site settings, user info etc. so in this case we require to share a varible across the all views of our application.

In laravel there is many methods to share the varibles across the views like using middleware, providers or base controller etc.

In this article i will show you to share variables between all views in laravel 8.

So to use it in your application you call directly in AppServiceProvider boot function as below

View::share('key', 'value');

Let’s take an example step by step

Step 1 : Create a controller

Create the controller and add the necessary imports and class. You can create by Laravel artisan or manually.

php artisan make:controller ArticleController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Article; 



class ArticleController extends Controller
{

    public function share1(Request $request)
    {
        return view("articles.share1");
    }

    public function share2(Request $request)
    {
        return view("articles.share2");
    }

    }
}

Here, we create 2 methods to share the same varibles in both methods.

Step 2: Create blade file for view

Create two file as we called in our controller one share1.blade.php and other is share2.blade.php

<h1>Readerstacks shared {{$sharekey}} here</h1>

and second

<h1>Readerstacks shared 2 {{$sharekey}} here</h1>

In above code we have $sharekey and if you noticed we have not assigned this key in out controller.

Step 3: Create routes

Now, open the routes/web.php and add below routes

<?php

use App\Http\Controllers\ArticleController;
use Illuminate\Support\Facades\Route;


Route::get('/share-variable1',[ArticleController::class, 'share1']);
Route::get('/share-variable2',[ArticleController::class, 'share2']);

Step 3: Create shared variable in AppServiceProvider

Now, open app\Porviders\AppServiceProvider.php and write in boot method

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
      
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        
        View::share('sharekey', 'This is shared');
        
    }
}

As you can see i have imported use Illuminate\Support\Facades\View; and used in boot method as below

 View::share('sharekey', 'This is shared');

now run the test and check our implementation by running artisan serve command in terminal

php artisan serve

Screenshot

  • Screenshot 2022 01 15 at 1.54.23 PM
  • Screenshot 2022 01 15 at 1.54.16 PM 1
Share variable between views laravel

Related

Php Laravel laravelphpshare variable

Post navigation

Previous post
Next post

Related Posts

Php How to create multiple size thumbs from image in laravel

How to create multiple size thumbs from image in laravel 8 ?

May 14, 2022February 22, 2024

Resizing images can be a best performance booster for any application since this will load the proper size images. In this article i will show you to create multiple size thumbs from image in laravel using intervention package. Big size of image can reduce the performance of the application therefor…

Read More
Php How to Search JSON Data in Database Laravel

How to Search JSON Data in Database Laravel ?

August 2, 2022March 16, 2024

JSON Data in database is used to store the informational data. JSON can be easily parse and stringify so it can be stored easily in database. In this post i will show you to search JSON data in database laravel. Laravel by default configured with MySQL and we will store…

Read More
Php How to Get Current Url in Blade File Laravel

How to Get Current Url in Blade File Laravel ?

October 27, 2022March 16, 2024

Sometimes in our application we want to Get Current Url in Blade file Laravel because we want to add specific conditions to show the data on behalf of current url. It can be complete url, route name and query params too so that we can fetch the data accordingly and…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version