Ad

Edit Table With Many To Many Relationship In Laravel

I have problems in updating a bridge table resulting from a many to many relationship, if I select a new course it adds it without problems but if I leave any course unchecked it does not delete it, how can I proceed?

This is the form

<div class="container">
  <div class="row">
    <div class="col-lg-12">
      <form class="form-group" action="{{route('trainers.update', $trainer->id)}}" method="post" enctype="multipart/form-data">
          @csrf
          @method('PUT')

          <div class="form-group">
            <label for="name">Nome</label>
            <input type="text" class="form-control" name="name" value="{{$trainer->name}}">
          </div>

          <div class="form-group">
            <label for="surname">Cognome</label>
            <input type="text" class="form-control" name="surname" value="{{$trainer->surname}}">
          </div>

          <div class="form-group">
            <label for="description">Descrizione</label>
            <textarea class="form-control" name="description" rows="8" cols="80">{!! $trainer->description !!}</textarea>
          </div>

          @foreach ($courses as $course)
            <div class="form-check form-check-inline mb-2">
              <input name="course_id[]" class="form-check-input" type="checkbox" value="{{$course->id}}">
              <label class="form-check-label" for="course_id">{{$course->name_course}}</label>
            </div>
          @endforeach

          <div class="form-group">
            <img src="{{asset('storage/'.$trainer->image)}}" alt="">
          </div>

          <div class="custom-file">
            <input type="file" class="custom-file-input" name="image">
            <label class="custom-file-label" for="image">Scegli un'immagine</label>
            <div class="invalid-feedback"><strong>N.B.</strong> dimensione consigliata 160px x 160px</div>
          </div>

          <div class="form-group">
            <input type="submit" class="form-control" value="MODIFICA ISTRUTTORE">
          </div>
        </form>
    </div>
  </div>
</div>

This is the edit function in controller

    public function edit($id)
    {
        $trainer = Trainer::find($id);
        $courses = Course::all();
        return view('trainers.edit', compact('trainer','courses'));
    }

This is the update function in controller

    public function update(Request $request, Trainer $trainer)
    {
      $data = $request->all();

      $trainer->update($data);

      $trainer->courses()->detach($data['course_id']);
      $trainer->courses()->attach($data['course_id']);

      return redirect()->route('trainers.admin');
    }
Ad

Answer

try this

public function edit($id)
{
    $trainer = Trainer::find($id);
    $courses = Course::where('active', 1)->get(); //assume active is there in courses table (or any other column)

    // $trainer->courses - retrive trainer courses
    return view('trainers.edit', compact('trainer','courses'));
}

public function update(Request $request, Trainer $trainer)
{
      $data = $request->all();
      $trainer->update($data);

      $trainer->courses()->sync($data['course_id']);

      return redirect()->route('trainers.admin');
}

sync method accepts an array of IDs to place on the intermediate table.

For more information read: Syncing Associations

You can also refer Filtering Relationships Via Intermediate Table Columns from above link

Ad
source: stackoverflow.com
Ad