I'm the beginner of laravel 6. I just want to ask. I want to display data but it doesn't show.
index.blade.php:
@if(isset($teachers))
@foreach($teachers->qualifs as $qualif)
<li>{{ $qualif->qual }}</li>
@endforeach
@endif
Controller:
public function index()
{
$teachers= DB::table('teachers')->first();
$qualifs = DB::table('qualifs')->find($teachers->id);
return view('teachers.index',compact('teachers','qualifs'));
}
qualif.php:
public function teachers()
{
return $this->belongsToMany('todolist\teacher', 'qualif_teachers');
}
teacher.php:
public function qualifs()
{
return $this->belongsToMany('todolist\qualif', 'qualif_teachers');
}
Note: Data is storing correctly, only displaying issue.
ERROR:Undefined property: stdClass::$qualifs
relationship belongs to a model instance that means to an eloquent object. when you are using query builder you will get stdObject instead of an eloquent object. and thus your relationship is not working. to make this work you have to use eloquent instead of query builder.
public function index()
{
$teachers= teacher::get();
return view('teachers.index',compact('teachers'));
}
and view will be like
@foreach($teachers as $teacher)
@foreach($teacher->qualifs as $qualif)
<li>{{ $qualif->qual }}</li>
@endforeach
@endforeach