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.
Answer
Solution:
Change it:
to
and try again.
Explanation: The second parameter of
view()
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
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 use
compact()
which will make code much cleaner.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
and then return
that did the trick..
thank you...
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)
Answer
Solution:
You can pass data in
return view
have two way :OR