How to avoid foreach-loop because raw Laravel query is used to get a single record detail. When I tried without foreach-loop, the following error occurred "Property [name] does not exist on this collection instance".
Controller
public function show(Student $student)
{
$result = DB::table('students')
->select('*')
->where('id', "=" ,$student['id'])
->get();
foreach($result as $studentData){}
return view('student.show', ['studentData' => $studentData]);
}
Blade View
{{ $studentData->name}}
CodePudding user response:
->get()
always returns a Collection, even if there's only one match. If you only want the first record, you can use ->first()
instead.
CodePudding user response:
Do you really have to use raw queries? I assume Student is a Laravel Model, in that case, you should be able to use Student::find($id)
to get the model directly.