简体   繁体   中英

How can i get data from intermediate table (many to many relationship) in laravel

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');
    }

This is Trainer Model

<?php

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();
    }

}

This is Course Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Course extends Model
{
  protected $fillable = ['name_course','description_course','logo_course'];


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

This is Trainer Model

public function courses(){
  return $this->belongsToMany('App\Course', 'course_trainer ', 'trainer_id', 'course_id')->withTimestamps();
}

This is Course Model

public function trainers(){
  return $this->belongsToMany('App\Trainer', 'course_trainer ', 'course_id', 'trainer_id')->withTimestamps();
}

belongsToMany() method 4 parameters, the first one is the location of your model to link, the second is the name of the pivot table, the third one is the current model foreign key and the fourth one is the other's model foreign key.

now you can do something like this in the form

<input name="course_id[]" class="form-check-input" type="checkbox" @if($course->pivot->trainer_id == $trainer->id) "checked='' " @endif value="{{$course->id}}">

I solved the problem without changing the model ... it was enough to do this

          @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}}" @foreach ($trainer->courses as $value)
                {{($course->id == $value->pivot->course_id) ? 'checked' : ''}}

              @endforeach>

              <label class="form-check-label" for="course_id">{{$course->name_course}}</label>

            </div>
          @endforeach

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM