I would like to read the contents of the intermediate table between the trainers and courses table to be able to flag the chackboxes of the active courses for a given trainer, how can I do?
Here you can look the trainers table http://prntscr.com/nhcrl4
Here you can look the courses table http://prntscr.com/nhcrvp
Here you can look the course_trainer (intermediate) table http://prntscr.com/nhcsek
I thought of using the ternary, you advise me?
thanks a lot
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()->sync($data['course_id']);
return redirect()->route('trainers.admin');
}