How to Pass Data to All Views in Laravel 5

How to pass data to all views in Laravel 5?

This target can achieve through different method,

1. Using BaseController

The way I like to set things up, I make a BaseController class that extends Laravel’s own Controller, and set up various global things there. All other controllers then extend from BaseController rather than Laravel’s Controller.

class BaseController extends Controller
{
public function __construct()
{
//its just a dummy data object.
$user = User::all();

// Sharing is caring
View::share('user', $user);
}
}

2. Using Filter

If you know for a fact that you want something set up for views on every request throughout the entire application, you can also do it via a filter that runs before the request — this is how I deal with the User object in Laravel.

App::before(function($request)
{
// Set up global user object for views
View::share('user', User::all());
});

OR

You can define your own filter

Route::filter('user-filter', function() {
View::share('user', User::all());
});

and call it through simple filter calling.

Update According to Version 5.*

3. Using Middleware

Using the View::share with middleware

Route::group(['middleware' => 'SomeMiddleware'], function(){
// routes
});



class SomeMiddleware {
public function handle($request)
{
\View::share('user', auth()->user());
}
}

4. Using View Composer

View Composer also help to bind specific data to view in different ways. You can directly bind variable to specific view or to all views. For Example you can create your own directory to store your view composer file according to requirement. and these view composer file through Service provide interact with view.

View composer method can use different way, First example can look alike:

You could create an App\Http\ViewComposers directory.

Service Provider

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
public function boot() {
view()->composer("ViewName","App\Http\ViewComposers\TestViewComposer");
}
}

After that, add this provider to config/app.php under "providers" section.

TestViewComposer

namespace App\Http\ViewComposers;

use Illuminate\Contracts\View\View;

class TestViewComposer {

public function compose(View $view) {
$view->with('ViewComposerTestVariable', "Calling with View Composer Provider");
}
}

ViewName.blade.php

Here you are... {{$ViewComposerTestVariable}}

This method could help for only specific View. But if you want trigger ViewComposer to all views, we have to apply this single change to ServiceProvider.

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
public function boot() {
view()->composer('*',"App\Http\ViewComposers\TestViewComposer");
}
}

Reference

Laravel Documentation

For Further Clarification Laracast Episode

If still something unclear from my side, let me know.

How to pass data to all views in Laravel 5.6?

So, what I did was in the AppServiceProvider class, in the boot function I added this.

      View::composer('*', function ($view) {


if(!\Auth::check())
{

return;

}

$userType = \Auth::user()->user_type ;



if($userType == config('constant.student'))
{
$chats = studentChat();

}
else if($userType == config('constant.teacher'))
{
$chats = teacherChat();

}


$view->with('chats', $chats);

});

Laravel How to pass data in all views included in main template file?

get categories in app/Providers/AppServiceProvider.php boot method



namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

use Illuminate\Support\Facades\Auth;
use DB;


class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
View::share('key', 'value');
Schema::defaultStringLength(191);

$categories=DB::table('categories')->get();
View::share('categories',$categories);

}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}

now you can access categories in all views and controllers in $categories variable

in your footer :

@foreach($categories as $category)
<p>{{$category->name}}</p>
@endforeach

How can i pass Category::all(); to every view

You could do something like this in the constructor of your base controller

$categories=Category::all();
View::share('categories', $categories);

Here is more info in the laravel docs

https://laravel.com/docs/7.x/views#sharing-data-with-all-views

how to add common variable for laravel and pass this data to all view all views

According to Laravel documentation you can use view composer:

https://laravel.com/docs/5.8/views#view-composers

for using this feature in app > AppServiceProvider and in boot method you can use this approach for passing parameter to specific view If you have data that you want to be bound to a view each time that view is rendered. this approach meet your need perfectly. for example you want to pass a parameter named userName to header.

View()->composer('header', function ($view){
$userName= "username"
$view->with(['userName'=>$userName]);
});

Passing data to all views served by a controller in Laravel

Use View::share on the class construct function:

use View;

class NavController extends Controller {
function __construct() {
View::share('tags', Tag::all());
}
public function posts()
{
$posts = Post::all();
return view('posts')->with('posts', => $posts);
}
public function users()
{
$users = User::all();
return view('users')->with('users', => $users);
}
}

how to pass same data to multiple views in laravel

If you're wanting to pass the same data to multiple views within your application you could use View Composers

E.g. in the boot() method of your AppServiceProvider you would have something like:

public function boot()
{
view()->composer(['home', 'profile'], function ($view) {

$notifications = \App\Notification::all(); //Change this to the code you would use to get the notifications

$view->with('notifications', $notifications);
});
}

Then you would just add the different blade file names (like you would with a route) to the array.


Alternatively, you could share the notifications with all views:

public function boot()
{
$notifications = \App\Notification::all(); //Change this to the code you would use to get the notifications

view()->share('notifications', $notifications);
}

How to pass helper data to all views in Laravel 5 using helper in AppServiceProvider?

Since you pass the variable by reference in your helper function, you need to define them before calling. Change your view share code to this.

public function boot()
{
$cart = null;
$total = 0;

helperFunctions::getPageInfo($cart, $total);

$data = [
'cart' => $cart,
'total' => $total,
];

View::share($data);
}

You can then access $cart and $total in all your views.

Alternatively if you want to make things cleaner, make the helper function return the array of data and pass it to the view.



Related Topics



Leave a reply



Submit