bashiro's avatar

Eloquent query Return last data as null

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 )

Any help please ? Thanks in Advance

0 likes
6 replies
Snapey's avatar

please format your code blocks

if your code errors then please post the specific error

if it is failing when you request a specific url then the rest of your question is irrelevant

bashiro's avatar

Thanks for the answer.

This is the error I get;

ErrorException PHP 8.3.2 10.48.11 Attempt to read property "id" on null

At the href below: This happens only on the last data in the database is empty: Any help please? But does not happen when it is not empty

<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>
tykus's avatar

@bashiro are you really using the same ID for both StudentExam and Student models?

$singleStudent = StudentExams::find($student_id)?->student;
//
$examsrecordSingle =Student::find($student_id)?->examsrecord;

Why do you need to find the Student through the StudentExcams model when you have queried the Student model directly? Anyway, your template assumes that $singleStudent is not null; it can be, so code accordingly, e.g.

@if ($singleStudent)
<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>
@endif

You really should take one of the many free introductio to Laravel series here.

bashiro's avatar

@tykus Yes I am using the same ID for both. There is a relation Your second question: The reason is The student has the profile whiles the studentExams hold the exams results. I will try your suggestion.

bashiro's avatar

@tykus Thanks a lot using the @if ($singleStudent) solved the issue. Very grateful!

tykus's avatar

@bashiro mark the thread solved if you're all set.

And... consider taking the time to learn how to use the framework with the free video lessons here.

1 like

Please or to participate in this conversation.