Support the ongoing development of Laravel.io →
Blade Database Eloquent

Hello folks, Thanks for any help. I am still learning Laravel. I am passing data to blade and in a table format where I have actions of edit and delete buttons in the table. So far so good. There is another button on the blade file to print pdf . Everything works fine with the exception of the last data in the table row. When I try to click on the button in the last row eloquent throws an exception

null // app\Http\Controllers\StudentExamsControlle The rest of the data in the above row works or responds perfectly. Any help? I used (using dd) for foe test

Controller code

public function singleStudent ($student_id){ $singleStudent = StudentExams::find($student_id)?->student;; $examsrecordSingle =Student::find($student_id)?->examsrecord;; dd( $singleStudent, $examsrecordSingle); return view ('exams.registered_result',compact('singleStudent', 'examsrecordSingle')); There is a relation in the two Models above

In the blade file code

<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-gray-800">Transcript Preview</h1>
 <a class="btn btn-primary" href="{{url('student/exams/pdf/'.$singleStudent->id)}}" role="button">View / Print Final Transcript</a>
  </div>

@foreach ($examsrecordSingle as$student )

<tr> <td class="text-center">{{ $key+1 }}</td> <!--where to place subjects--> <td class="text-center">{{$student->exams_type}}</td> <td class="text-center">{{$student->exams_year}}</td> <td class="text-center">{{$student->subject_name}}</td> <td class="text-center">{{$student->exams_date}}</td> <td class="text-center">{{$student->obtained_marks }}</td> <td class="text-center">{{$student->grade}}</td> </tr> @endforeach

Any help please ? Thanks in Advance

0
  1. Use named routes instead of URLs directly as it makes your application easier to maintain and understand

First, define a named route in your web.php:

Route::get('student/exams/pdf/{student}', 'StudentExamsController@printPDF')->name('student.exams.pdf');

Then, update your Blade template to use the route:

<a class="btn btn-primary" href="{{ route('student.exams.pdf', $singleStudent->id) }}" role="button">View / Print Final Transcript</a>

  1. In your Blade template, Handle Potential Null Objects

@if ($singleStudent) <a class="btn btn-primary" href="{{ route('student.exams.pdf', $singleStudent->id) }}" role="button">View / Print Final Transcript</a> @endif

And similarly, check before your foreach loop:

@if ($examsrecordSingle) @foreach ($examsrecordSingle as $key => $student) <!-- Table row --> @endforeach @endif

  1. Check relations one more time to be sure it works, as expected
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Bashiro bashirosu Joined 14 Mar 2024

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.