Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Čamo's avatar
Level 3

Laravel 8 cant get session variables in AppServiceProvider

I would like to share some variables from session in all views in Laravel 8. According documentation I call View::share() method in AppServiceProvider:

<?php

namespace App\Providers;

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

class AppServiceProvider extends ServiceProvider
{

    public function register()
    {
        //
    }

    public function boot()
    {    
        // Variables shared in all views.
        View::share('showModal', session('showModal'));
    }
}

The problem is that although session showModal key is really set, I cant get it in AppServiceProvider::boot(). If I call session('showModal') in controller on in view I can see the correct value. Only AppServieProvider returns null. Can somebody explain me please what is wrong with this code? Thanks a lot.

0 likes
2 replies

Please or to participate in this conversation.