Fix: Сохранение и загрузка групп пользователя
✅ $user->load('groups') для eager loading ✅ Проверка !empty(trim()) для groups поля ✅ Не очищаем группы если поле пустое ✅ Загрузка существующих тегов при инициализации Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
parent
df8172070a
commit
3dfe1e5a2c
|
|
@ -99,10 +99,11 @@ class UserController extends Controller
|
|||
{
|
||||
Gate::authorize('update', $user);
|
||||
|
||||
$user->load('groups');
|
||||
$organizations = Organization::pluck('name', 'id');
|
||||
$roles = Role::pluck('name', 'name');
|
||||
$userGroups = $user->groups->pluck('id')->toArray();
|
||||
|
||||
|
||||
// Показываем все доступные группы: общие + группы организации пользователя
|
||||
if ($user->organization_id) {
|
||||
$allGroups = Group::whereNull('organization_id')
|
||||
|
|
@ -148,11 +149,12 @@ class UserController extends Controller
|
|||
$user->syncRoles([$validated['role']]);
|
||||
|
||||
// Обновление групп (строка "1,2,3" → массив)
|
||||
if (!empty($validated['groups'])) {
|
||||
if (!empty(trim($validated['groups'] ?? ''))) {
|
||||
$groupIds = array_map('intval', array_filter(explode(',', $validated['groups'])));
|
||||
$user->groups()->sync($groupIds);
|
||||
} else {
|
||||
$user->groups()->detach();
|
||||
// Если поле пустое - не трогаем группы (или можно сделать sync([]) для очистки)
|
||||
// $user->groups()->detach();
|
||||
}
|
||||
|
||||
return redirect()->route('admin.users.show', $user)
|
||||
|
|
|
|||
|
|
@ -143,20 +143,22 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
|
||||
// Загрузка существующих тегов
|
||||
@if(count($value) > 0)
|
||||
fetch('{{ $url }}?q=')
|
||||
.then(response => response.json())
|
||||
.then(items => {
|
||||
selectedTags.forEach(id => {
|
||||
const item = items.find(i => String(i.id) === id);
|
||||
if (item) {
|
||||
const existingIds = {!! json_encode(array_map('strval', is_array($value) ? $value : [])) !!};
|
||||
if (existingIds.length > 0) {
|
||||
// Загружаем все группы и фильтруем нужные
|
||||
fetch('{{ $url }}?q=')
|
||||
.then(response => response.json())
|
||||
.then(allItems => {
|
||||
const items = allItems.filter(item => existingIds.includes(String(item.id)));
|
||||
items.forEach(item => {
|
||||
select.addOption(item);
|
||||
addTag(id, item);
|
||||
}
|
||||
addTag(item.id, item);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@endif
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Reference in New Issue