簡體   English   中英

從控制器傳遞3個參數以查看Laravel

[英]Passing 3 parameters from controller to view Laravel

我試圖將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

任何人都可以指出我正確的方向。 謝謝

您需要在第二個參數中將數據作為數組傳遞:

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

要么:

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

這里有兩個問題首先是你沒有使用正確的語法來傳遞變量來查看這里 ,第二個是你沒有傳遞你在控制器中獲取的相同變量。

嘗試這個,

在你的控制器中:

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'));

        }

然后你可以保持你的視圖代碼相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM