【问题标题】:Passing 3 parameters from controller to view Laravel从控制器传递 3 个参数以查看 Laravel
【发布时间】:2017-10-12 19:33:45
【问题描述】:

我正在尝试将 3 个参数从控制器传递到视图。出于某种原因,我只能发送 2 个参数,它给出了一个错误提示

未定义变量:第三个参数

我的路线

Route::get('/notification_list/notification_application/{user_id}/{notification_id}', 'AdminController@adminGetEachNotification')->name('show.notification_application');

我的控制器

public function adminNotificationList(){

        //$notifications = Notification::all() ;
        $data = [];
        $data['notifications'] = Notification::where(['approved'=>0])->get();

        $approvedData = [];
        $approvedData['approvednotifications'] = Notification::where(['approved'=>3])->get();

        $sendToAdmin = [];
        $sendToAdmin['sendtoadmin'] = Notification::where(['approved'=>1])->get();
        //dd($sendApproval);
        return view('Notification.notification_admin', $data, $approvedData, $sendToAdmin);
    }

我的看法

@extends('layouts.app')
@section('title',  '| Notification')
@section('content')

<!-- Page content -->
<div id="page-content-wrapper">
<!-- Keep all page content within the page-content inset div! -->
<div class="page-content inset">

  <div class="row">
    <div class="col-xs-12">
      <ul>
        <h1>Notifications</h1>
      </ul>

    </div>
  </div>

  <br/>

  <h2>New Application</h2>
  <div class="row">
    <div class="col-xs-12">
      <table border="1">
        <thead>
          <tr>
            <td>Notification ID</td>
            <td>Name of Applicant</td>
            <td>Applied date</td>
            <td>Approve</td>
          </tr>
        </thead>
        <tbody>
          @foreach ($notifications as $notification)
          <tr>
            <td>{{ $notification->id }}</td>
            <td><a href="/admin/notification_list/notification_application/{{$notification->user->id}}/{{$notification->id}}">{{ $notification->user->name }}</a></td>
            <td>{{$notification->created_at->todatestring()}}</td>
            <td>{{$notification->approved}}</td>
          </tr>
          @endforeach
        </tbody>
      </table>
    </div>
  </div>

  <br/>

  <h2>Approved Application</h2>
  <div class="row">
    <div class="col-xs-12">
      <table border="1">
        <thead>
          <tr>
            <td>Notification ID</td>
            <td>Name of Applicant</td>
            <td>Applied date</td>
            <td>Approved</td>
            <td>Approved Date</td>
          </tr>
        </thead>
        <tbody>
          @foreach($approvednotifications as $approvednotification)

          <tr>
            <td>{{ $approvednotification->id }}</td>
            <td><a href="/home/notification/notification_application/{{$approvednotification->user->id}}/{{$approvednotification->id}}">{{ $approvednotification->user->name }}</a></td>
            <td>{{$approvednotification->created_at->todatestring()}}</td>
            <td>Approved</td>
          </tr>

          @endforeach
        </tbody>
      </table>
    </div>
  </div>

  <h2>Pending Approval from Super Admin</h2>

  <div class="row">
    <div class="col-xs-12">
      <table border="1">
        <thead>
          <tr>
            <td>Notification ID</td>
            <td>Name of Applicant</td>
            <td>Applied date</td>
            <td>Approved</td>
            <td>Approved Date</td>
          </tr>
        </thead>
        <tbody>
          @foreach($sendtoadmin as $send)

          <tr>
            <td>{{ $send->id }}</td>
            <td><a href="/home/notification/notification_application/{{$send->user->id}}/{{$send->id}}">{{ $send->user->name }}</a></td>
            <td>{{$send->created_at->todatestring()}}</td>
            <td>Approved</td>
          </tr>

          @endforeach
        </tbody>
      </table>
    </div>
  </div>

  </div>
  </div>
</div>
@stop

谁能指出我正确的方向。谢谢

【问题讨论】:

  • 有冲突,你粘贴 adminNotificationList 方法和 adminGetEachNotification 路由。纠正这个可以帮助你
  • 对不起。这是我的正确路线Route::get('/notification_list', 'AdminController@adminNotificationList')-&gt;name('admin.notification_list');
  • 请不要在这里用工作代码覆盖您的问题,因为这基本上会使下面帮助过您的人的工作无效。保持问题不变,如果您想展示如何使用答案来解决您的特定情况,请添加另一个答案。谢谢。

标签: php laravel parameter-passing laravel-eloquent


【解决方案1】:

您需要在第二个参数中将数据作为数组传递:

return view('Notification.notification_admin', compact('data', 'approvedData', 'sendToAdmin'));

或者:

return view('Notification.notification_admin', [
    'data' => $data,
    'approvedData' => $approvedData,
    'sendToAdmin' => $sendToAdmin
]);

【讨论】:

  • 现在它给出错误未定义变量:通知
  • @Mill3r 因为你没有传递这个变量。您正在传递$data,因此您可以将notifications 作为单独的变量传递,也可以在视图中使用$data['notifications']
【解决方案2】:

这里有两个问题,第一是您没有使用正确的语法将变量传递给查看 see here,第二是您没有传递在控制器中获取的相同变量。

试试这个,

在您的控制器中:

public function adminNotificationList(){  

            $notifications = Notification::where(['approved'=>0])->get();


            $approvednotifications = Notification::where(['approved'=>3])->get();

            $sendtoadmin = Notification::where(['approved'=>1])->get();

            return view('Notification.notification_admin', compact('notifications ', 'approvednotifications ', 'sendToAdmin'));

        }

然后你可以保持你的视图代码不变。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-27
    • 2021-04-19
    • 1970-01-01
    • 2017-03-03
    • 2017-07-27
    • 2017-02-08
    • 2015-01-20
    • 1970-01-01
    相关资源
    最近更新 更多