|2||Changes in AppServiceProvider|
|3||Access variable in view file|
In this tutorial we will discuss about how to access variable in all view files , while working on large web applications and also while working with dynamic web-applications where we have multiple view files with diffrent folder structures and there are some varibles like company details which we have to show dynamically from backend at header and footer or we can take example of menu which we always need on header section then we have options like to call varible directly from view file but that doesn't seems to good practice as a develper so there we access that variables through app\Providers\AppServiceProvider.
In app\Providers\AppServiceProvider under the public function boot() we create our variable and share with our all view file as shown below
AppServiceProvider code :Copy
Also read:HOW TO MAKE TRAITS IN LARAVEL 9
As shown in code in app\Providers\AppServiceProvider.initally import the view facade and then simply send the variable with view property as shown we are using simple example you may use proper function which is used to get data from backend.
Now call the view file by route
web.php code :Copy
Also read:HELPER OR COMMON FUNCTION IN LARAVEL
Now we can call the $name varibale in any view file
code for view file :Copy