2

I am trying to pass 3 parameters from the controller to the view. For some reason i can only send 2 parameters and it gives an error saying

Undefined variable: 3rd parameter

my route

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

My controller

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

my view

@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

Can anyone point me to the right direction. thank you

halfer
  • 19,824
  • 17
  • 99
  • 186
Mill3r
  • 544
  • 1
  • 10
  • 31
  • there's conflict, you paste **adminNotificationList** method and **adminGetEachNotification** route. correct this to can help you – MohamedSabil83 May 13 '17 at 17:04
  • sorry. This is my correct route `Route::get('/notification_list', 'AdminController@adminNotificationList')->name('admin.notification_list');` – Mill3r May 13 '17 at 17:26
  • Please do not overwrite your questions with working code here, since that essentially invalidates the work of people who have helped you below. Keep the question as it is, and if you would like to show how you used an answer to fix your particular case, please add another answer. Thanks. – halfer May 13 '17 at 19:08
  • This might help you http://stackoverflow.com/questions/20110757/laravel-pass-more-than-one-variable-to-view/36114696#36114696 – Akshay Khale May 13 '17 at 20:06

2 Answers2

4

You need to pass data as an array in the second parameter:

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

Or:

return view('Notification.notification_admin', [
    'data' => $data,
    'approvedData' => $approvedData,
    'sendToAdmin' => $sendToAdmin
]);
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • now it gives error Undefined variable: notifications – Mill3r May 13 '17 at 16:34
  • @Mill3r because you're not passing this variable. You're passing `$data`, so you could pass `notifications` as a separate variable or you can use `$data['notifications']` in the view. – Alexey Mezenin May 13 '17 at 17:45
0

There are two problems here first is you are not using the right syntax to pass variables to view see here and the second is you are not passing the same variables you fetched in your controller.

try this,

In your controller:

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

        }

and then you can keep your view code the same as is.

Sapnesh Naik
  • 11,011
  • 7
  • 63
  • 98