Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


How to Sharing Data with all Views ?

Answers were Sorted based on User's Feedback



How to Sharing Data with all Views ?..

Answer / nafees ahmad

We have seen how we can pass data to views but at times, there is a need to pass data to all the views.
Laravel makes this simpler, There is a method called share() which can be used for this purpose.
The share() method will take two arguments, key and value.
Typically share() method can be called from boot method of service provider.
We can use any service provider, AppServiceProvider or our own service provider.
Example :
Observe the following example to understand more about sharing data with all views -

Add the following line in app/Http/routes.php file.
app/Http/routes.php

Route::get('/test', function(){
return view('test');
});

Route::get('/test2', function(){
return view('test2');
});
Step 2 - Create two view files — test.php and test2.php with the same code. These are the two files which will share data. Copy the following code in both the files. resources/views/test.php & resources/views/test2.php
<html>
<body>
<h1><?php echo $name; ?></h1>
</body>
</html>
Step 3 - Change the code of boot method in the file app/Providers/AppServiceProvider.php as shown below.

(Here, we have used share method and the data that we have passed will be shared with all the views.) app/Providers/AppServiceProvider.php

Is This Answer Correct ?    0 Yes 0 No

How to Sharing Data with all Views ?..

Answer / Amit Kumar Gill

To share data across multiple views in Laravel, you can use the 'share' method within the AppServiceProvider. Here is an example:
1. Define a property (e.g., $siteName) in the AppServiceProvider class.
2. Set a default value for the property (if required).
3. Use the 'make' and 'singleton' methods to create and reuse the instance of the provider across all routes/views.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Laravel PHP Framework Interview Questions

What is database migration. How to create migration via artisan?

1 Answers  


Explain important directories used in a common laravel application.

1 Answers  


What is eager loading in laravel?

1 Answers  


How to work with Database in Laravel ?

2 Answers  


How can you display html with blade in laravel?

1 Answers  


How to enable or disable maintaince mode in laravel.

1 Answers  


Explain about laravel project?

1 Answers  


What is orm laravel use?

1 Answers  


What are the main differences between laravel 4 and laravel 5.x?

1 Answers  


What is a recursive helper function?

1 Answers  


In laravel, what is guarded attribute in a model?

1 Answers  


How to do 301 redirects in laravel?

1 Answers  


Categories