add Department resource

This commit is contained in:
aslan 2024-02-08 15:59:44 +03:00
parent 941da60c6f
commit 953a6b3631
14 changed files with 451 additions and 6 deletions

View File

@ -0,0 +1,75 @@
<?php
namespace App\Http\Controllers\Catalog;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreDepartmentRequest;
use App\Http\Requests\UpdateDepartmentRequest;
use App\Models\Department;
use App\Models\Faculty;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse;
class DepartmentController extends Controller
{
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$departments = Department::all();
return view('catalog.department.index', compact('departments'));
}
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$faculties = Faculty::pluck('name', 'id');
return view('catalog.department.create', compact('faculties'));
}
public function store(StoreDepartmentRequest $request): RedirectResponse
{
$validated = $request->validated();
$department = new Department();
$department->name = $validated['name'];
$department->description = $validated['description'];
$department->position = $validated['position'];
$department->faculty_id = $validated['faculty_id'];
$department->save();
return redirect()->route('departments.index');
}
public function show(Department $department): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
return view('catalog.department.show', compact('department'));
}
public function edit(Department $department): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$faculties = Faculty::pluck('name', 'id');
return view('catalog.department.edit', compact('department', 'faculties'));
}
/**
* Update the specified resource in storage.
*/
public function update(UpdateDepartmentRequest $request, Department $department)
{
$validated = $request->validated();
$department->name = $validated['name'];
$department->description = $validated['description'];
$department->position = $validated['position'];
$department->faculty_id = $validated['faculty_id'];
$department->save();
return redirect()->route('departments.index');
}
public function destroy(Department $department): RedirectResponse
{
$department->delete();
return redirect()->route('departments.index');
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
class StoreDepartmentRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
'position' => 'required|int|max:255',
'name' => 'required|string|max:255|unique:educational_institutions,name',
'description' => 'string',
'faculty_id' => 'required|int'
];
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
class UpdateDepartmentRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'position' => 'int|max:255',
'description' => 'string',
'faculty_id' => 'int|required',
'name' => [
'required',
'string',
'max:255',
"unique:educational_institutions,name,{$this->department->id}",
],
];
}
}

24
app/Models/Department.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Department extends Model
{
use HasFactory;
protected $fillable = [
'id',
'name',
'description',
'position',
];
public function faculty(): BelongsTo
{
return $this->belongsTo(Faculty::class);
}
}

View File

@ -0,0 +1,66 @@
<?php
namespace App\Policies;
use App\Models\Department;
use App\Models\User;
use Illuminate\Auth\Access\Response;
class DepartmentPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
//
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, Department $department): bool
{
//
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
//
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, Department $department): bool
{
//
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Department $department): bool
{
//
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, Department $department): bool
{
//
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, Department $department): bool
{
//
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Department>
*/
class DepartmentFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
//
];
}
}

View File

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('departments', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description');
$table->integer('position');
$table->foreignId('faculty_id')->constrained('faculties');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('departments');
}
};

View File

@ -0,0 +1,17 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DepartmentSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
//
}
}

View File

@ -84,7 +84,7 @@
@endforeach
<tr class=" border border-white border-bottom-0 border-start-0 border-end-0">
<td colspan="3"><div class="mb-2">
<a href="{{ route('files.create', $receptionScreen->id) }}"
<a href="{{ route('files_create', $receptionScreen->id) }}"
class="btn btn-primary">
Добавить файл
</a>
@ -104,7 +104,7 @@
<tr>
<td colspan="3">
@php($idReceptionScreen = $receptionScreen->id)
<a href="{{ route('files.create', $idReceptionScreen) }}" class="btn btn-primary">Добавить
<a href="{{ route('files_create', $idReceptionScreen) }}" class="btn btn-primary">Добавить
файл</a>
</td>
</tr>

View File

@ -0,0 +1,64 @@
@extends('layouts.admin-layout')
@section('content')
@auth()
<div class="row">
<div class="col">
<h1 class=""> Создать кафедру</h1>
{{ Form::open(['url' => route('departments.store'), 'method' => 'POST', 'class' => '']) }}
<div class="col">
<div class="mt-3">
{{ Form::label('position', 'Позиция') }}
</div>
<div class="mt-1">
{{ Form::text('position', '', ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('position') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('name', 'Название') }}
</div>
<div class="mt-1">
{{ Form::text('name', '', ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('name') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('description', 'Описание') }}
</div>
<div class="mt-1">
{{ Form::text('description', '', ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('description') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('faculty_id', 'Факультет') }}
</div>
<div class="mt-1">
{{ Form::select('faculty_id', $faculties, null, ['class' => 'form-select']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('faculty_id') }}
@endif
</div>
<div class="mt-3">
{{ Form::submit('Создать', ['class' => 'btn btn-primary']) }}
</div>
</div>
{{ Form::close() }}
</div>
</div>
@endauth
@endsection

View File

@ -0,0 +1,43 @@
@extends('layouts.admin-layout')
@section('content')
<div class="container">
<h2>Кафедры</h2>
<br>
<a href="{{ route('departments.create') }}" class="btn btn-primary">Создать Кафедру</a>
<br>
<br>
<table class="table">
<thead class="border-b-2 border-solid border-black text-left" style="text-align: left">
<tr>
<th scope="col">Позиция</th>
<th scope="col">Название</th>
<th scope="col">Описание</th>
<th scope="col">Факультет</th>
<th scope="col">действия</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
@foreach($departments as $department)
<tr class="">
<th scope="row">{{ $department->position }}</th>
<td><a href="{{ route('departments.show', $department) }}">{{ $department->name }}</a></td>
<td>{{ Str::words($department->description, 10, '...') }}</td>
<td>{{ $department->faculty->name }}</td>
<td>
<a href="{{ route("departments.edit", $department) }}"
class="btn btn-secondary">редактировать</a>
<a rel="nofollow" data-method="delete" data-confirm="Вы действительно хотите удалить?"
href="{{ route('departments.destroy', $department) }}"
class="btn btn-danger">удалить</a>
</td>
</tr>
@endforeach
</tbody>
</table>
<br>
<br>
</div>
@endsection

View File

@ -0,0 +1,31 @@
@php
use App\Models\Department;
use App\Models\Faculty;
use Illuminate\Support\Collection;
/** @var Collection|Department[] $department */
$faculty = Faculty::find($department->faculty->id)
@endphp
@extends('layouts.admin-layout')
@section('content')
@auth()
<h6>
<a href="{{ route('educational-institutions.show', $faculty->educationalInstitution) }}">
{{ $faculty->educationalInstitution->name }}
</a>-> <a href="{{ route('faculties.show', $faculty) }}">{{ $faculty->name }}</a>
-> {{ $department->name }}
</h6>
<div class="container mt-4">
<h2>Название</h2>
<p>{{ $department->name }}</p>
<h2>Описание</h2>
<p>{{ $department->description }}</p>
<h2>Позиция</h2>
<p>{{ $department->position }}</p>
{{-- <h2>Факультеты</h2>--}}
{{-- @foreach($department->faculties as $faculty)--}}
{{-- <p><a href="{{ route('faculties.show', $faculty) }}">{{ $faculty->name }}</a></p>--}}
{{-- @endforeach--}}
</div>
@endauth
@endsection

View File

@ -1,13 +1,14 @@
<?php
use App\Http\Controllers\EducationalInstitutionController;
use App\Http\Controllers\FacultyController;
use App\Http\Controllers\Catalog\DepartmentController;
use App\Http\Controllers\Catalog\EducationalInstitutionController;
use App\Http\Controllers\Catalog\FacultyController;
use App\Http\Controllers\FileController;
use App\Http\Controllers\ReceptionScreenController;
use App\Http\Controllers\UserController;
Route::middleware(['auth', 'verified'])->group(function () {
Route::get('/files/create/{file?}', [FileController::class, 'create'])->name('files.create');
Route::get('/files/create/{file?}', [FileController::class, 'create'])->name('files_create');
Route::get('/files/download/{file}', [FileController::class, 'download'])->name('files.download');
Route::resources([
@ -16,5 +17,6 @@ Route::middleware(['auth', 'verified'])->group(function () {
'/admin-reception-screen' => ReceptionScreenController::class,
'/educational-institutions' => EducationalInstitutionController::class,
'/faculties' => FacultyController::class,
'/departments' => DepartmentController::class,
]);
});

View File

@ -71,7 +71,7 @@ Route::get('/general-information', function () {
return view('menu.inostrannym-abiturientam.obshchie-svedeniya');
})->name('obshchie-svedeniya');
Route::get('/departments', function () {
Route::get('/departments-list', function () {
return view('menu.inostrannym-abiturientam.kafedry');
})->name('kafedry');