Php 如果选择了多个图像,Laravel sync会在透视表中生成重复的日期
如果我选择了多个图像,为什么同步会在透视表中进行重复同步 在我的应用程序中,当添加新竞赛时,用户可以选择一个或多个图像/文档,文件路径将保存在文件表中,并将数据同步到Php 如果选择了多个图像,Laravel sync会在透视表中生成重复的日期,php,arrays,laravel,pivot-table,laravel-7,Php,Arrays,Laravel,Pivot Table,Laravel 7,如果我选择了多个图像,为什么同步会在透视表中进行重复同步 在我的应用程序中,当添加新竞赛时,用户可以选择一个或多个图像/文档,文件路径将保存在文件表中,并将数据同步到竞赛\u文件透视表中 这是创建用户界面 这是我的控制器上的存储功能 public function store($competition, Request $request, Team $team) { if ($request->has('photos') && is_arra
竞赛\u文件
透视表中
这是创建用户界面
这是我的控制器上的存储功能
public function store($competition, Request $request, Team $team)
{
if ($request->has('photos') && is_array($request->photos)) {
$files = $this->filesRepo->getByUuids($request->photos);
$fileId = $files->pluck('id')->toArray();
if ($files->isNotEmpty()) {
$forSync = array_fill_keys($fileId, ['competition_id' => $competition,'team_id' => $request->team,'type' => $request->type,'share_type' => $request->share_type]);
$team->documents()->sync($forSync);
}
}
return redirect(route('documents.index',$competition))->with('success', 'Document updated.');
}
这是我模型中的关系代码
public function documents()
{
return $this->belongsToMany(File::class,'competition_file','team_id','file_id')->wherePivot('type', 'document');
}
当我选择以下多个图像时,它会在competition\u文件
表中复制一个图像
这就是它在competition\u文件中使用重复数据保存透视表的方式
但是如果在我选择了两个图像时在sync
之前转储数据,它只显示两个数组代码,请参见下面的代码
public function store($competition, Request $request, Team $team)
{
if ($request->has('photos') && is_array($request->photos)) {
$files = $this->filesRepo->getByUuids($request->photos);
$fileId = $files->pluck('id')->toArray();
if ($files->isNotEmpty()) {
$forSync = array_fill_keys($fileId, ['competition_id' => $competition,'team_id' => $request->team,'type' => $request->type,'share_type' => $request->share_type]);
dd($forSync);
$team->documents()->sync($forSync);
}
}
return redirect(route('documents.index',$competition))->with('success', 'Document updated.');
}
结果
如果我删除转储
并重新加载同一页面,它将正确同步
如果我在没有转储的情况下重试,并且如果我选择了两个图像并保存,它会创建一个副本吗
我需要知道是什么在创建重复同步
我希望我的问题很清楚,有人能帮我吗。为了以后的访客
公共功能商店($competition,Request$Request,Team$Team)
{
if($request->has('photos')&&is_数组($request->photos)){
$files=$this->filerepo->getByUuids($request->photos);
$fileId=$files->pull('id')->toArray();
如果($files->isNotEmpty()){
$forSync=array\u fill\u key($fileId[
“competition_id”=>$competition,
“类型”=>$request->type,
“共享类型”=>$request->share\u类型
]);
//如果隐式路由模型绑定不起作用
//如果$team为null或需要显式设置团队
//由用户选择,但未作为路由参数传递
团队::findOrFail($request->Team)->documents()->sync($forSync);
}
}
返回重定向(路由('documents.index',$competition))
->使用('success','Document updated');
}
将DB级别的唯一约束设置为唯一竞争团队文件(即,['competition\u id','team\u id','file\u id']),然后继续您的工作。您需要通过检查是否已经存在相同的文件(可能是自定义规则类)来更改请求验证,但您将保护数据库不受垃圾数据的影响,这是(应该是)最终目标。您不需要在数组填充键中使用'team\u id'=>$request->team
,由于同步是通过团队对象上的文档
关系进行的,因此应填写团队idautomatically@Donkarnash为什么团队id会自动填充?而用户在添加时需要选择一个团队files@Donkarnash用户可以在添加文件时选择团队1或团队2,这就是我使用'team\u id'=>$request->team
@Donkarnash的原因,但如果我删除$forSync=array\u fill\u键($fileId),['competition\u id'=>$competition,'type'=>$request->type,'share\u type'=>$request->share\u type]);
并提交的团队id
将同步为空,可能是什么问题?