Laravel

How to send a data to all views in laravel

It is sometimes necessary to send data or an array to all the views sometimes. but this is the rare case.

For this problem, you can have this solution

1) To send the data to all the
i) go to app\providers\appServiceProvider
ii) in boot() function do the following
ii) if you want to send a data say your name ‘sam’ to all the view write the following in boot() function

view::share('name','sam');

here name is a key and sam is a value

iii) import the view by using

use View;

iv) in the view you just have to call the key

{{name}}

 

the output will be sam

2) To send data of the array
a) you need to import view obviously and the Model. In my case user(say)

use View;
use App\User;

b) the next step is to send the data to the views

view::share('users',User::orderBy('id','desc')->get();

c) now in the view you need to put in the loop and get all the data that are in array form

@foreach($users as $user)
{{$user->name}}
@endforeach

 

jaminrai

Share
Published by
jaminrai

Recent Posts

Skype Closes Its Doors After 20 Years: A Look Back and the Apps That Faded with It

After 20 years of transforming global communication, Skype has officially ceased operations. Microsoft, which purchased…

2 months ago

The Duck and the Tortoise: A Tale of Patience and Teamwork

In a serene forest by the edge of a sparkling lake, there lived a cheerful…

3 months ago

10 Smart Strategies for a Secure and Enjoyable Retirement

Retiring comfortably is a goal many aspire to achieve, and with the right strategies, it’s…

3 months ago

Troubleshooting a Slow Computer

Is your computer running frustratingly slow? Don't worry; you're not alone in facing this common…

3 months ago

The Clever Fox and the Greedy Crow

Once, in a sunny forest, Fox and Crow were friends. One day, Crow found a…

3 months ago