Skip to content

Commit

Permalink
Исправление бага при обновлении словаря
Browse files Browse the repository at this point in the history
  • Loading branch information
delaynore committed May 29, 2024
1 parent 0b62f01 commit 409a3e1
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions app/Http/Controllers/DictionaryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function store(Request $request): RedirectResponse
'fk_user_id' => $request->user()->id,
]);
$dictionary->save();
if(!empty($validated['tags'])) {
if (!empty($validated['tags'])) {
foreach ($validated['tags'] as $tag) {
$tagModel = Tag::find($tag);
$dictionary->tags()->attach($tagModel->id, ['id' => Str::uuid()]);
Expand Down Expand Up @@ -114,7 +114,7 @@ public function edit(string $dictionaryId): View

return view(
'dictionary.edit',
compact('dictionary')
compact('dictionary')
);
}

Expand All @@ -139,18 +139,17 @@ public function update(Request $request, string $dictionaryId): RedirectResponse
$dictionary->description = $validated['description'];
$dictionary->visibility = $validated['visibility'];

$newTags = $validated['tags'];
// Detach tags that are not present in the new list
$dictionaryTags = $dictionary->tags()->pluck('tags.id')->toArray();
foreach ($dictionaryTags as $id) {
if (!in_array($id, $validated['tags'])) {
$dictionary->tags()->detach($id);
if (!empty($validated['tags'])) {
$dictionaryTags = $dictionary->tags()->pluck('tags.id')->toArray();
foreach ($dictionaryTags as $id) {
if (!in_array($id, $validated['tags'])) {
$dictionary->tags()->detach($id);
}
}
}
// Attach tags that are present in the new list but not in the existing dictionary
foreach ($validated['tags'] as $tagId) {
if (!in_array($tagId, $dictionaryTags)) {
$dictionary->tags()->attach($tagId, ['id' => Str::uuid()]);
foreach ($validated['tags'] as $tagId) {
if (!in_array($tagId, $dictionaryTags)) {
$dictionary->tags()->attach($tagId, ['id' => Str::uuid()]);
}
}
}

Expand Down

0 comments on commit 409a3e1

Please sign in to comment.