Laravel 如果选择了多张图片,同步会在数据透视表中创建一个重复的日期 table
Laravel sync makes a duplicate date in pivot table if multiple images selected
如果我 select 多于图像,为什么同步会在枢轴 table 中进行重复同步?
在我的应用程序中,当添加新比赛时,用户可以 select 一个或多个 images/documents 并且文件路径将保存在文件 table 中并将数据同步到competition_file
枢轴 table.
这是创建 UI
这是我控制器上的存储功能
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');
}
当我 select 有多个图像时,它会在 competition_file
table
这就是它在 competition_file
数据透视 table 中保存重复数据的方式
但是如果在 sync
之前转储数据,而我 select 编辑了两个图像,它只显示两个数组,请参见下面的代码
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.');
}
结果
如果我删除 Dump
并重新加载同一页面,它会正确同步
如果我在没有 Dump
的情况下重试并且如果我 select 有两张图片并保存它会创建一个副本?
我需要知道是什么导致了重复同步。
我希望我的问题很清楚,有人可以帮我解决一下吗。
仅供后续访问者使用。
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,
'type' => $request->type,
'share_type' => $request->share_type
]);
//If the implicit route model binding is not working
//if $team is null or you need to explicitly set the team
//selected by user and which is not passed as route param
Team::findOrFail($request->team)->documents()->sync($forSync);
}
}
return redirect(route('documents.index',$competition))
->with('success', 'Document updated.');
}
如果我 select 多于图像,为什么同步会在枢轴 table 中进行重复同步?
在我的应用程序中,当添加新比赛时,用户可以 select 一个或多个 images/documents 并且文件路径将保存在文件 table 中并将数据同步到competition_file
枢轴 table.
这是创建 UI
这是我控制器上的存储功能
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');
}
当我 select 有多个图像时,它会在 competition_file
table
这就是它在 competition_file
数据透视 table 中保存重复数据的方式
但是如果在 sync
之前转储数据,而我 select 编辑了两个图像,它只显示两个数组,请参见下面的代码
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.');
}
结果
如果我删除 Dump
并重新加载同一页面,它会正确同步
如果我在没有 Dump
的情况下重试并且如果我 select 有两张图片并保存它会创建一个副本?
我需要知道是什么导致了重复同步。
我希望我的问题很清楚,有人可以帮我解决一下吗。
仅供后续访问者使用。
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,
'type' => $request->type,
'share_type' => $request->share_type
]);
//If the implicit route model binding is not working
//if $team is null or you need to explicitly set the team
//selected by user and which is not passed as route param
Team::findOrFail($request->team)->documents()->sync($forSync);
}
}
return redirect(route('documents.index',$competition))
->with('success', 'Document updated.');
}