将变量传递给视图时出错(laravel 5.3)

时间:2017-01-09 06:31:28

标签: php laravel

我在那里创建了一个通知控制器,我做了一个方法

public function getNotificationsView(){
    $ = UserNotifications::orderBy('id', 'desc')->get();
    return $notifications;
}

现在这个通知变量我想在我的header.blade.php中使用它,因为我试图这样做会给出错误 我的header.blade.php

<li class="dropdown dropdown-extended dropdown-notification" id="header_notification_bar">
    <a href="{{ route('user.notification') }}" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-close-others="true">
        <i class="icon-bell"></i>
        <span class="badge badge-default"> 7 </span>
    </a>
    <ul class="dropdown-menu">
        <li>
            <ul class="dropdown-menu-list scroller" style="height: 250px;" data-handle-color="#637283">
                @foreach($notifications as $users)
                <li>
                    <a href="javascript:;">
                        <span class="time">just now</span>
                        <span class="details">
                            <span class="label label-sm label-icon label-success">
                                <i class="fa fa-plus"></i>
                        </span> {{$users->title}} </span>
                    </a>
                </li>
                @endforeach
            </ul>
        </li>
    </ul>
</li>

路线为

Route::any('/notification', ['as' => 'user.notification', 'uses' => 'NotificationController@getNotificationsView']);

它给出了“错误:未定义的变量通知”

3 个答案:

答案 0 :(得分:0)

您没有将任何变量传递给视图。这样做:

public function getNotificationsView()
{
    $notifications = UserNotifications::orderBy('id', 'desc')->get();
    return view('notifications', compact('notifications'));
}

或者:

public function getNotificationsView()
{
    $notifications = UserNotifications::orderBy('id', 'desc')->get();
    return view('notifications', ['notifiactions' => $notifications]);
}

答案 1 :(得分:0)

您应该在return子句中添加刀片。

public function getNotificationsView(){
        $notifications = UserNotifications::orderBy('id', 'desc')->get();
        return View::make('blade name', ['notifications' => $notifications]);
    }

答案 2 :(得分:0)

将以下两行替换为getNotificationsView()

$data['notifications'] = UserNotifications::orderBy('id', 'desc')->get();
return view('viewname')->withdata($data);

并在view.Blade文件中添加以下代码:

@foreach($notifications as $users)
 ..... write html code .....
@endforeach