Search code examples
phpmysqllaraveleloquentmany-to-many

Populate table with many to many relationship in Laravel


i have 3 tables: trainers, courses, course_trainer

when I create a new trainer I have to link to it the courses it plays, how can I pass the id of the trainer just created and the id of the course to the bridge table course_trainer?

Currently I can only pass a single id for the course, but I can't pass the id of the trainer and I can't get a new record (report) created for each course chosen


<div class="container">
  <div class="row">
    <div class="col-lg-12">
      <form class="form-group" action="{{route('trainers.store')}}" method="post" enctype="multipart/form-data">
          @csrf
          @method('POST')

          <div class="form-group">
            <label for="name">Nome</label>
            <input type="text" class="form-control" name="name" placeholder="Inserisci il nome dell'istruttore">
          </div>

          <div class="form-group">
            <label for="surname">Cognome</label>
            <input type="text" class="form-control" name="surname" placeholder="Inserisci il cognome dell'istruttore">
          </div>

          <div class="form-group">
            <label for="description">Descrizione</label>
            <textarea class="form-control" name="description" rows="8" cols="80"></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="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="INSERISCI ISTRUTTORE">
          </div>
        </form>
    </div>
  </div>
</div>
public function store(Request $request)
    {
        $data = $request->all();

        $image = Storage::disk('public')->put('trainers',$data['image']);

        $newTrainer = New Trainer();

        $newTrainer->name = $data['name'];
        $newTrainer->surname = $data['surname'];
        $newTrainer->description = $data['description'];
        $newTrainer->image = $image;

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

        $newTrainer->save();

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

Solution

  • Hello guys i have solved the problem, in create.blade.php in name of input checkboxes i added the square brackets to the name

              @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
    

    and I positioned

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

    after

    $newTrainer->save();
    

    now everything works very well

    thank you all for your help