dimanche 27 octobre 2019

many to many relationship error in laravel

i have two tables,

1) qualifs

2) teachers

one teacher can have many qualifications

i created

pivot table; qualif_teacher

with two columns (teacher_id & qualif_id)

when i am saving teacher info, teachers qualifications are saving correctly with multiple ids, my problem is i am getting error when i try to view any teachers qualification in my blade file.

error: Undefined variable: teacher

Route: /teachers

Index Controller:

public function index()
{
  $teachers= DB::table('teachers')
  $qualifs = DB::table('qualifs')->find($teachers);
return view('teachers.index',compact('teachers','qualifs'));
}

Edit Controller:

public function edit($id)
  {
    $qualifs = DB::table('qualifs')->find($id);
      $teacher = Teacher::find($id);
      return response()->json([
          'status' => 'success',
          'teacher' => $teacher,
          'qualifs'=>$qualifs,
      ]);

  }

View:

@if(isset($teacher))
  @foreach($teacher->qualifs as $qualif)
      <li></li>
      @endforeach
@endif


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/32T5e66
via IFTTT

Aucun commentaire:

Enregistrer un commentaire