0

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');
    }
amepro
  • 19
  • 4
  • 2
    Try saving your trainer first before attaching the course. – thisiskelvin Apr 25 '19 at 14:11
  • hey bro, have publish your model Trainer – Joan Marcos Apr 25 '19 at 14:27
  • as @thisiskelvin said, you might need to save your new `trainer` first since we need for it to have an `id` (automatically assigned when storing to the database) to store in the `course_trainer`. – Julio Motol Apr 25 '19 at 14:58
  • Thanks guys, now you enter both ids but always and only insert a single record even though I have selected 3 courses this is the trainer model namespace App; use Illuminate\Database\Eloquent\Model; class Trainer extends Model { protected $fillable = ['name','surname','description','image']; public function courses(){ return $this->belongsToMany(Course::class)->withTimestamps(); } } – amepro Apr 25 '19 at 20:21
  • Check [this](https://stackoverflow.com/questions/24702640/laravel-save-update-many-to-many-relationship) q/a. – Tpojka Apr 26 '19 at 01:15
  • i have read q/a but doesn't work...ca you help me? – amepro Apr 26 '19 at 07:10

2 Answers2

0

Thanks guys, now you enter both ids but always and only insert a single record even though I have selected 3 courses

this is the trainer model

    namespace App;

use Illuminate\Database\Eloquent\Model;

class Trainer extends Model
{
    protected $fillable = ['name','surname','description','image'];

    public function courses(){
      return $this->belongsToMany(Course::class)->withTimestamps();
    }

}
amepro
  • 19
  • 4
0

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

amepro
  • 19
  • 4