dthtvk3666 2019-04-26 19:39
浏览 414

如何从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');
    }
  • 写回答

3条回答 默认 最新

  • doushou8299 2019-04-27 07:14
    关注

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题