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