Posted in

Laravel pasar variables a todas las vistas

Si queremos pasar variables que tienen que estar en algunas vistas y queremos ahorrarnos el tener que pasarlas a cada una lo podemos hacer de forma global:

View::composer(array(
'user.newsletters-list'
), function($view){
$view->with(array(
'notification_count' => Notification::where('user_id', Auth::user()->id)
->where('revised', 0)
->count(),
'notifications' => Notification::where('user_id', Auth::user()->id)
->where('revised', 0)
->get()
));
});

Si lo que queremos es pasar las variables a todas las vistas y no tener que indicar el nombre de las mismas lo haremos de la siguiente forma:

View::share(array(
    'notification_count' => Message::where('to_user', Auth::user()->id)
        ->where('read', 0)
        ->count(),
    'notifications' => Message::with('from_users')
        ->where('to_user', Auth::user()->id)
        ->where('read', 0)
        ->get(),
));

Este código se puede añadir o bien en el BaseController o si afecta a un solo controlador lo podríamos poner en él (al principio del archivo)

Deixa un comentari

L'adreça electrònica no es publicarà. Els camps necessaris estan marcats amb *


The reCAPTCHA verification period has expired. Please reload the page.