Feat: CRUD групп с типами (организация/общие)

 create.blade.php — форма создания с выбором типа группы
 store метод — сохранение группы
 index.blade.php — кнопка создать, фильтр по пользователю
 edit метод — фильтр пользователей по организации
 Ссылка Группы в сайдбаре для Admin/Manager
 Полные маршруты для groups

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
mirivlad 2026-03-30 10:21:17 +08:00
parent d27b631c8f
commit 6599b8d5b6
5 changed files with 142 additions and 69 deletions

View File

@ -24,7 +24,17 @@ class GroupUserController extends Controller
$query = Group::with(['organization', 'users']);
if ($request->filled('organization_id')) {
$query->where('organization_id', $request->organization_id);
if ($request->organization_id === 'general') {
$query->whereNull('organization_id');
} else {
$query->where('organization_id', $request->organization_id);
}
}
if ($request->filled('user_id')) {
$query->whereHas('users', function($q) use ($request) {
$q->where('users.id', $request->user_id);
});
}
$groups = $query->orderBy('name')->paginate(20);
@ -33,6 +43,43 @@ class GroupUserController extends Controller
return view('admin.groups.index', compact('groups', 'organizations'));
}
public function create()
{
Gate::authorize('create', Group::class);
$organizations = Organization::pluck('name', 'id');
return view('admin.groups.create', compact('organizations'));
}
public function store(Request $request)
{
Gate::authorize('create', Group::class);
$validated = $request->validate([
'group_type' => 'required|in:organization,general',
'organization_id' => 'nullable|exists:organizations,id',
'name' => 'required|string|max:255',
'description' => 'nullable|string',
'is_active' => 'boolean',
]);
// Для группы организации organization_id обязательна
if ($validated['group_type'] === 'organization' && empty($validated['organization_id'])) {
return back()->withErrors(['organization_id' => 'Выберите организацию для группы'])->withInput();
}
Group::create([
'organization_id' => $validated['group_type'] === 'organization' ? $validated['organization_id'] : null,
'name' => $validated['name'],
'description' => $validated['description'] ?? null,
'is_active' => $validated['is_active'] ?? true,
]);
return redirect()->route('admin.groups.index')
->with('success', 'Группа успешно создана.');
}
public function show(Group $group)
{
Gate::authorize('view', $group);
@ -48,7 +95,15 @@ class GroupUserController extends Controller
Gate::authorize('update', $group);
$group->load(['organization', 'users']);
$users = User::where('organization_id', $group->organization_id)->get();
// Получаем доступных пользователей для этой группы
if ($group->organization_id) {
// Группа организации — только пользователи этой организации
$users = User::where('organization_id', $group->organization_id)->get();
} else {
// Общая группа — все пользователи
$users = User::all();
}
return view('admin.groups.edit', compact('group', 'users'));
}

View File

@ -1,91 +1,92 @@
@extends('layouts.app')
@section('title', 'Добавить группу')
@section('title', 'Создать группу')
@section('content')
<div class="container-fluid">
<div class="row">
<nav class="col-md-3 col-lg-2 d-md-block sidebar collapse">
<div class="position-sticky pt-3">
@include('partials._sidebar')
</div>
</nav>
<nav class="col-md-3 col-lg-2 d-md-block sidebar"><div class="position-sticky pt-3">@include('partials._sidebar')</div></nav>
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 main-content">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Добавить группу</h1>
<a href="{{ route('admin.organizations.show', $organization) }}" class="btn btn-secondary btn-sm">
<i class="bi bi-arrow-left"></i> Назад
</a>
<div class="d-flex justify-content-between align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Создать группу</h1>
<a href="{{ route('admin.groups.index') }}" class="btn btn-secondary btn-sm">Назад</a>
</div>
@if ($errors->any())
<div class="alert alert-danger">
<ul class="mb-0">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('admin.organizations.groups.store', $organization) }}" method="POST">
<form action="{{ route('admin.groups.store') }}" method="POST">
@csrf
<div class="row">
<div class="col-md-8">
<div class="card shadow-sm mb-4">
<div class="col-md-8 mb-4">
<div class="card shadow-sm">
<div class="card-header bg-primary text-white"><h5 class="mb-0">Основная информация</h5></div>
<div class="card-body">
<h5 class="card-title mb-3">Основная информация</h5>
<div class="mb-3">
<label class="form-label">Тип группы *</label>
<select name="group_type" id="groupType" class="form-select @error('group_type') is-invalid @enderror" required onchange="toggleOrganizationField()">
<option value="">Выберите тип</option>
<option value="organization" {{ old('group_type') == 'organization' ? 'selected' : '' }}>Группа организации</option>
<option value="general" {{ old('group_type') == 'general' ? 'selected' : '' }}>Общая группа</option>
</select>
<small class="text-muted">Группа организации только пользователи этой организации. Общая любые пользователи.</small>
@error('group_type')<div class="invalid-feedback">{{ $message }}</div>@enderror
</div>
<div class="mb-3" id="organizationField" style="display:none;">
<label class="form-label">Организация *</label>
<select name="organization_id" class="form-select @error('organization_id') is-invalid @enderror">
<option value="">Выберите организацию</option>
@foreach($organizations as $id => $name)
<option value="{{ $id }}" {{ old('organization_id') == $id ? 'selected' : '' }}>{{ $name }}</option>
@endforeach
</select>
@error('organization_id')<div class="invalid-feedback">{{ $message }}</div>@enderror
</div>
<div class="mb-3">
<label for="name" class="form-label">Название группы <span class="text-danger">*</span></label>
<input type="text" class="form-control @error('name') is-invalid @enderror"
id="name" name="name" value="{{ old('name') }}" required autofocus>
@error('name')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
<label class="form-label">Название группы *</label>
<input type="text" name="name" class="form-control @error('name') is-invalid @enderror" value="{{ old('name') }}" required>
@error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
</div>
<div class="mb-3">
<label for="description" class="form-label">Описание</label>
<textarea class="form-control @error('description') is-invalid @enderror"
id="description" name="description" rows="3">{{ old('description') }}</textarea>
@error('description')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
<label class="form-label">Описание</label>
<textarea name="description" class="form-control" rows="3">{{ old('description') }}</textarea>
</div>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="is_active" name="is_active" value="1" {{ old('is_active', true) ? 'checked' : '' }}>
<label class="form-check-label" for="is_active">Активна</label>
<div class="form-check mb-3">
<input type="checkbox" name="is_active" value="1" class="form-check-input" {{ old('is_active', true) ? 'checked' : '' }}>
<label class="form-check-label">Активна</label>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card shadow-sm mb-4">
<div class="col-md-4 mb-4">
<div class="card shadow-sm">
<div class="card-header bg-info text-white"><h5 class="mb-0">Подсказка</h5></div>
<div class="card-body">
<h5 class="card-title mb-3">Организация</h5>
<p class="text-muted mb-0">{{ $organization->name }}</p>
@if($organization->inn)
<p class="text-muted small">ИНН: {{ $organization->inn }}</p>
@endif
<h6>Группа организации</h6>
<p class="small text-muted">Привязана к конкретной организации. Можно добавлять только пользователей этой организации.</p>
<hr>
<h6>Общая группа</h6>
<p class="small text-muted">Не привязана к организации. Можно добавлять любых пользователей системы.</p>
</div>
</div>
</div>
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary">
<i class="bi bi-check-lg"></i> Создать группу
</button>
<a href="{{ route('admin.organizations.show', $organization) }}" class="btn btn-secondary">
<i class="bi bi-x-lg"></i> Отмена
</a>
</div>
<button type="submit" class="btn btn-primary">Создать группу</button>
<a href="{{ route('admin.groups.index') }}" class="btn btn-secondary">Отмена</a>
</form>
</main>
</div>
</div>
<script>
function toggleOrganizationField() {
const type = document.getElementById('groupType').value;
document.getElementById('organizationField').style.display = (type === 'organization') ? 'block' : 'none';
}
document.addEventListener('DOMContentLoaded', function() {
toggleOrganizationField();
});
</script>
@endsection

View File

@ -7,6 +7,9 @@
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 main-content">
<div class="d-flex justify-content-between align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Группы пользователей</h1>
@can('create', App\Models\Group::class)
<a href="{{ route('admin.groups.create') }}" class="btn btn-primary btn-sm"><i class="bi bi-plus-lg"></i> Создать группу</a>
@endcan
</div>
@if(session('success'))<div class="alert alert-success">{{ session('success') }}</div>@endif
@if(session('error'))<div class="alert alert-danger">{{ session('error') }}</div>@endif
@ -14,15 +17,24 @@
<div class="card shadow-sm mb-4">
<div class="card-body">
<form action="{{ route('admin.groups.index') }}" method="GET" class="row g-3">
<div class="col-md-6">
<div class="col-md-4">
<select name="organization_id" class="form-select">
<option value="">Все организации</option>
<option value="">Все группы</option>
<option value="general" {{ request('organization_id') === 'general' ? 'selected' : '' }}>Общие группы</option>
@foreach($organizations as $id => $name)
<option value="{{ $id }}" {{ request('organization_id') == $id ? 'selected' : '' }}>{{ $name }}</option>
@endforeach
</select>
</div>
<div class="col-md-3">
<div class="col-md-4">
<select name="user_id" class="form-select">
<option value="">Все пользователи</option>
@foreach(\App\Models\User::pluck('name', 'id') as $id => $name)
<option value="{{ $id }}" {{ request('user_id') == $id ? 'selected' : '' }}>{{ $name }}</option>
@endforeach
</select>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-primary w-100"><i class="bi bi-search"></i></button>
</div>
</form>

View File

@ -19,6 +19,11 @@
<i class="bi bi-people"></i> Пользователи
</a>
</li>
<li class="nav-item">
<a class="nav-link {{ Str::startsWith($currentRoute, 'admin.groups') ? 'active' : '' }}" href="{{ route('admin.groups.index') }}">
<i class="bi bi-people-fill"></i> Группы
</a>
</li>
<li class="nav-item">
<a class="nav-link {{ Str::startsWith($currentRoute, 'admin.course-categories') ? 'active' : '' }}" href="{{ route('admin.course-categories.index') }}">
<i class="bi bi-folder"></i> Категории курсов

View File

@ -49,6 +49,6 @@ Route::middleware('auth')->group(function () {
Route::resource('courses.tests', TestController::class);
Route::resource('tests.questions', QuestionController::class);
Route::resource('course-assignments', CourseAssignmentController::class);
Route::resource('groups', GroupUserController::class)->except(['create', 'store']);
Route::resource('groups', GroupUserController::class);
});
});