【发布时间】:2019-08-13 23:44:44
【问题描述】:
我有四个具有多对多关系的模型。
CrewTypePostFlight
在Crew 模型中:
public function crews()
{
return $this->belongsToMany(Crew::class,'crew_flight_post_type','flight_id','crew_id')
->withPivot(['post_id','type_id']);
}
public function posts()
{
return $this->belongsToMany(Post::class,'crew_flight_post_type','flight_id','post_id')
->withPivot(['crew_id','type_id']);
}
public function types()
{
return $this->belongsToMany(Type::class,'crew_flight_post_type','flight_id','type_id')
->withPivot(['crew_id','post_id']);
}
我用这些列创建了数据透视表crew_flight_post_type:
crew_idflight_idtype_idpost_idtotal_flight_time
如何通过create.blade.php 中的一个选择框发送post_id、flight_id、crew_id、type_id 查看并保存或更新此数据透视表?
FlightController的in Store方法
$flight->crews()->attach(request('cap'),['post_id'=>$request->get('?'),'type_id'=>$type_id]);
这是我对Flight 模型的create.blade.php 视图:
<select>
@foreach($crew as $id => $name)
<option value="{{ $id }}">{{ $name }}</option>
@endforeach
</select>
</div>
@endforeach
【问题讨论】:
标签: php laravel many-to-many pivot-table