I have a modal button associated with each student on a table. When I press the button I want to be able to fetch all results associated with the admission_num from another table. The code I have is below.
Here is my controller code
public function view_student_result($id)
{
$user= User::where('admission_num', $id)->get();
$resultdata = juniorResults::where('admission_num', $id)->get();
return view('backend.student_result_view', compact('resultdata', 'user'));
}
Here is my route
Route::get('view-student-result/{id}', [viewResultController::class, 'view_student_result'])->name('view_student_result');
Here is the button that links to the view result page
<a href="{{ route('view_student_result', $studentDatas->admission_num) }}"><i class="fas fa-print"></i></a>
When I used get(), I got this error Property [admission_num] does not exist on this collection instance. But when I use first() instead of get() the code was able to fetch just one result. below is the dd(collection)
To get all students with a specific Admission_num, you can use this statement:
$resultdata = juniorResults::where('Admission_num', 12345)->get();
Small hint: Instead of writing your url by hand, it is safer to use the name of your route:
<a href="{{ route('view_student_result', $studentDatas['id']) }}"></a>
If you want all rows you dont have to use first(). Instead of first what you returns only the first userobject you have to use all() or get.
$user= User::where('admission_num', $id)->get();
small note You screenshot from you database table admission_num is upper case the first letter. And in your eloquent query you have lower case.