我如何从Laravel的中间表(多对多关系)中获取数据

我想阅读培训师和课程表之间的中间表的内容,以便能够为给定培训师标记活动课程的书架,我该怎么办?

在这里,您可以查看培训 师表http://prntscr.com/nhcrl4

在这里您可以查看课程表 http://prntscr.com/nhcrvp

在这里,您可以查看course_trainer(中级)表 http://prntscr.com/nhcsek

我想到了使用三元,你建议我吗?

多谢

这是表格

<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>



这是控制器中的编辑功能

public function edit($id)

    {

        $trainer = Trainer::find($id);

        $courses = Course::all();


        return view('trainers.edit', compact('trainer','courses'));

    }

这是控制器中的更新功能


    public function update(Request $request, Trainer $trainer)

    {

      $data = $request->all();


      $trainer->update($data);


      $trainer->courses()->sync($data['course_id']);


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

    }


aluckdog
浏览 153回答 3
3回答

慕尼黑的夜晚无繁华

我在不改变模型的情况下解决了问题...就足够了&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach ($courses as $course)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-check form-check-inline mb-2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="course_id[]" class="form-check-input" type="checkbox" value="{{$course->id}}" @foreach ($trainer->courses as $value)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{($course->id == $value->pivot->course_id) ? 'checked' : ''}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label class="form-check-label" for="course_id">{{$course->name_course}}</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach

神不在的星期二

这是训练师模型<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Trainer extends Model{&nbsp; &nbsp; protected $fillable = ['name','surname','description','image'];&nbsp; &nbsp; public function courses(){&nbsp; &nbsp; &nbsp; return $this->belongsToMany(Course::class)->withTimestamps();&nbsp; &nbsp; }}这是课程模型<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Course extends Model{&nbsp; protected $fillable = ['name_course','description_course','logo_course'];&nbsp; public function trainers(){&nbsp; &nbsp; return $this->belongsToMany(Trainer::class)->withTimestamps();&nbsp; }}

德玛西亚99

这是训练师模型public function courses(){&nbsp; return $this->belongsToMany('App\Course', 'course_trainer ', 'trainer_id', 'course_id')->withTimestamps();}这是课程模型public function trainers(){&nbsp; return $this->belongsToMany('App\Trainer', 'course_trainer ', 'course_id', 'trainer_id')->withTimestamps();}belongsToMany() 方法4个参数,第一个是要链接的模型的位置,第二个是数据透视表的名称,第三个是当前模型的外键,第四个是另一个模型的外键。现在,您可以在表单中执行以下操作<input name="course_id[]" class="form-check-input" type="checkbox" @if($course->pivot->trainer_id == $trainer->id) "checked='' " @endif value="{{$course->id}}">
打开App,查看更多内容
随时随地看视频慕课网APP