Skip to content
Advertisement

Laravel query results not display on the blade properly

In my laravel application, I’m trying to display some records on my blade.

Following is my Controller function related to this issue.

public function show($id)
{
    $vrs = SampleRegistration::latest()->where('id','=',$id)->first();
    $trs = TestResult::all()->where('test_id','=',$id);
    //dd($trs);
    return view('viewresults.show',compact('vrs','trs'));
}

And I’m trying to display those data on my blade, following is my blade part.

<div class="col-xs-12 col-sm-12 col-md-12">
     <div class="form-group">
     @foreach ($trs as  $key => $tr)
         <strong>{{ $tr->test_type }}</strong>
         {{ $tr->test_result }}
     @endforeach    
     </div>
</div>

But now the issue is with,

$trs = TestResult::all()->where('test_id','=',$id);

This can have 1 or more results.

But, even though it has more results, every time this will display only one result on my blade.

And any of the time it won’t display the

<strong>{{ $tr->test_type }}</strong>

But when I DD it shows me all the results…

Advertisement

Answer

I found an answer for this, I just changed my query into this

$trs =DB::table('test_results')
            ->where('test_id','=',$id)
            ->get();

and now It displays all the records, still have no idea why the previous

$trs = TestResult::all()->where('test_id','=',$id);

was failing.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement