Global Data In All Laravel Blade Pages


08th August 2020 1 min read
Share On        


Most of the time you might require some generic data which needs to be made available across all the Laravel Blade pages.


For example, you might need a count of categories in your sidebar.


Adding Generic View Data In AppServiceProvider.php


To make data available generic across all the view pages one of the simplest ways is to add it in AppServiceProvider.php boot function as shown below


app/Providers/AppServiceProvider.php - boot() method

public function boot()
{
    View::composer('*', function($view){
        /** You can also add with some condition */
        if(Session::has('user_details')){
            $view->with('projectCountDetails', ProjectsController::projectCountDetails());
        }
    });
}


From the above code, we will be able to access the projectCountDetails variable across all the view pages as shown below.


Conclusion


Hope this article helped you. Please share it with your friends.







AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing