A
A
axblue2016-11-24 23:51:38
Laravel
axblue, 2016-11-24 23:51:38

How to pass a variable to layouts?

Hello. Tell me how it's done. There is a controller and there are two variables (one variable is the records, the second is the number of records), from the controller I pass the first variable to the view, everything is ok, then I need to transfer the second variable to the layout (that is, to the main template) which includes the view in which I pass the first variable. How can I do that?
controller:

$user = User::find(1);
        $unreadNotifications = $user->notifications()->unread()->get();
        $notifications = $user->notifications()->unread()->count();
        return view('user.notifications', [
            'unreadNotifications' => $unreadNotifications,
        ]);

$notificationsI need to pass in the layouts which includes the viewuser.notifications

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Евгений Вольф, 2016-11-24
@leshikgo

Оно?
View::share('key', $value);

H
he he, 2016-11-25
@tosha_lol_daaa

а что сложного?

return view('user.notifications', [
    'var' => $var1,
    'name' => $var2
]);

Еще один вариант (их куча):
return view('user.notifications', compact('unreadNotifications', 'notifications'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question