php - Cannot retrive data from the database with first() Laravel

824

My controller:

public function getNotificationApplication($user_id, $notification_id)
{
  $userdetails = Notification::with('user')->where(['user_id'=>$user_id])->first();
  $dataList = [];
  $dataList['notify'] = NotificationTypeA::with('notification')->where(['notification_id'=>$notification_id])->get();

  return view('Notification.notification_application', $userdetails, $dataList);
}

My route:

Route::get('/notification_application/{user_id}/{notification_id}', '[email protected]')->name('show.notification_application');

my view:

@foreach ($userdetails as $userdetail)
  {{$userdetail->storage_location}}
@endforeach

When I am retrieving data from the database, I get a

Undefined variable: userdetails

I can retrieve other variable ($data) that I am passing as an array, I can display them like this

@foreach ($notify as $notification)
  <tr>
      <td>{{ $notification->item_name }} </td>
      <td>{{ $notification->risk_level }}</td>
      <td>{{ $notification->quantity }}</td>
      <td>{{ $notification->volume }}</td>
      <td></td>
  </tr>
@endforeach

When I use

   `dd($userdetails);` 

in the controller I get the correct data.

i have no clue how to do this.

695

Answer

Solution:

Change it:

return view('Notification.notification_application', $userdetails, $dataList);

to

return view('Notification.notification_application', array('userdetails' => $userdetails, 'dataList' => $dataList));

and try again.

Explanation: The second parameter ofview() is an array in which we can pass thekey =>value pair. And on view, we can get the data using thekey passed in the array of second parameter ofview()

Reference

278

Answer

Solution:

You have to pass an array to the blade.

For that, you can use syntax as Mayank Pandeyz said or alternatively you can usecompact() which will make code much cleaner.

return view('Notification.notification_application', compact('userdetails' ,'dataList'));
874

Answer

Solution:

Thanks to Parth Vora and Mayank Pandeyz

i solved my problem combining both of their answers.

First of all i was passing 2 parameters one of which is an array the other is simply the first row of the table.

so i have to make a slight change in the controller

$userdetails['userdetails'] = Notification::with('user')->where(['user_id'=>$user_id])->first();

and then return

return view('Notification.notification_application', array('userdetails' => $userdetails), $dataList);

that did the trick..

thank you...

843

Answer

Solution:

you can use the following methods

1)return view('Notification.notification_application', compact('userdetails' ,'dataList'));

2)return view('Notification.notification_application', ['userdetails' => $userdetails , 'dataList' => $dataList]);

3)

return view('Notification.notification_application')
                ->with('userdetails', $userdetails)
                ->with('dataList, $dataList);
329

Answer

Solution:

You can pass data inreturn view have two way :

 return view('Notification.notification_application', compact('userdetails','dataList'));

OR

return view('Notification.notification_application',
                    [
                      'userdetails'=>$userdetails,
                      'dataList'=>$dataList
                    ]);

People are also looking for solutions to the problem: php - Get multiple post meta with WordPress REST API

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.