Php 如何从一个选择框发送多个数据以保存在数据透视表中?

Php 如何从一个选择框发送多个数据以保存在数据透视表中?,php,laravel,many-to-many,pivot-table,Php,Laravel,Many To Many,Pivot Table,我有四个具有多对多关系的模型。 机组人员类型Post航班 在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

我有四个具有多对多关系的模型。
机组人员
类型
Post
航班

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']);
    }
<select>
 @foreach($crew as $id => $name)
    <option value="{{ $id }}">{{ $name }}</option>
 @endforeach
</select>

 </div>
@endforeach
我使用以下列创建了透视表
crew\u flight\u post\u type
crew\u id
flight\u id
type\u id
post\u id
总飞行时间

如何通过
create.blade.php
中的一个选择框发送
post\u id
flight\u id
create.blade.php
查看并保存或更新此透视表

FlightController的存储方法

 $flight->crews()->attach(request('cap'),['post_id'=>$request->get('?'),'type_id'=>$type_id]);

这是我的
create.blade.php
视图,用于
Flight
模型:

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']);
    }
<select>
 @foreach($crew as $id => $name)
    <option value="{{ $id }}">{{ $name }}</option>
 @endforeach
</select>

 </div>
@endforeach

@foreach($id=>$name)
{{$name}
@endforeach
@endforeach

将机组保存到
飞行
模型时(根据示例),您需要将
机组
模型作为第一个参数传递,然后您可以将任何轴值作为第二个参数中的关联数组传递

/$flight=flight::find(?);
//$crew=crew::查找(?);
//将机组模型附加到飞行模型
$flight->crews()->附加($crew[
“post_id”=>?,
“type_id”=>?,
“总飞行时间”=>?,
]);
另外,我认为这是一个输入错误,但您在上面的问题中使用了
机组人员腿部后置类型
机组人员飞行后置类型
,我猜它们是同一个表

编辑:评论中的问题。

…如何在视图中发送帖子id和键入id

你有几个选择

  • 您可以直接发送,因为您已经有了这些文件,因为它们用于保存航班/机组关系
  • returnview('your-view',['post\u id'=>?,'type\u id'=>?]);
    
    {{$post\u id}
    {{$type_id}}
    
  • 或者你可以通过关系使用它们
  • returnview(“你的视图”,compact(“航班”);
    
    @foreach($flight->crews as$crew)
    {{$crew->post_id}
    {{$crew->type_id}
    @endforeach
    
  • 或者您可以通过另一个关系上的轴心来使用它们
  • returnview(“你的视图”,compact(“航班”);
    
    @foreach($flight->类型为$type)
    {{$type->pivot->post_id}
    @endforeach
    @foreach($flight->posts as$post)
    {{$post->pivot->type_id}
    @endforeach
    
    你说得对。这是个打字错误。表是crew_flight_post_type,但如何在视图中发送post_id和type_id。谢谢你的回答。我添加了一些编辑,可能会回答你的第一个问题。对不起,我不知道你在最后的评论中问了什么。