duande9301 2017-01-09 06:31
浏览 51

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

I make a notify controller there I made a method as

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

now this notifications variable i want to use it in my header.blade.php as i am trying to do it gives an error my 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 is as

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

It gives "Error: undefined variable notifications"

  • 写回答

3条回答 默认 最新

  • duancai9010 2017-01-09 06:33
    关注

    You're not passing any variables to the view. Do this:

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

    Or:

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

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)