From 00266b9fa00c374f4e88b08cfc4ecbf0e3432e71 Mon Sep 17 00:00:00 2001 From: aslan Date: Thu, 8 Feb 2024 16:01:40 +0300 Subject: [PATCH] Refactoring Faculty and Education Institution views --- .../EducationalInstitutionController.php | 3 ++- .../{ => Catalog}/FacultyController.php | 12 +++++++++++- app/Models/Faculty.php | 6 ++++++ .../views/catalog/faculty/index.blade.php | 4 ++-- .../views/catalog/faculty/show.blade.php | 19 +++++++++++++++++++ .../views/layouts/admin-layout.blade.php | 3 ++- 6 files changed, 42 insertions(+), 5 deletions(-) rename app/Http/Controllers/{ => Catalog}/EducationalInstitutionController.php (96%) rename app/Http/Controllers/{ => Catalog}/FacultyController.php (85%) create mode 100644 resources/views/catalog/faculty/show.blade.php diff --git a/app/Http/Controllers/EducationalInstitutionController.php b/app/Http/Controllers/Catalog/EducationalInstitutionController.php similarity index 96% rename from app/Http/Controllers/EducationalInstitutionController.php rename to app/Http/Controllers/Catalog/EducationalInstitutionController.php index 8da1451..ebdb5fe 100644 --- a/app/Http/Controllers/EducationalInstitutionController.php +++ b/app/Http/Controllers/Catalog/EducationalInstitutionController.php @@ -1,7 +1,8 @@ route('faculties.index'); } + public function show(Faculty $faculty): View|Application|Factory|\Illuminate\Contracts\Foundation\Application + { + return view('catalog.faculty.show', compact('faculty')); + } + public function edit(Faculty $faculty): View|Application|Factory|\Illuminate\Contracts\Foundation\Application { $educationalInstitutions = EducationalInstitution::pluck('name', 'id'); @@ -60,6 +67,9 @@ class FacultyController extends Controller public function destroy(Faculty $faculty): RedirectResponse { + if ($faculty->departments()->exists()) { + return back(); + } $faculty->delete(); return redirect()->route('faculties.index'); } diff --git a/app/Models/Faculty.php b/app/Models/Faculty.php index 5568657..5f65286 100644 --- a/app/Models/Faculty.php +++ b/app/Models/Faculty.php @@ -5,6 +5,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasMany; class Faculty extends Model { @@ -21,4 +22,9 @@ class Faculty extends Model { return $this->belongsTo(EducationalInstitution::class); } + + public function departments(): HasMany + { + return $this->hasMany('App\Models\Department', 'faculty_id'); + } } diff --git a/resources/views/catalog/faculty/index.blade.php b/resources/views/catalog/faculty/index.blade.php index 8ccea3c..9bf0f19 100644 --- a/resources/views/catalog/faculty/index.blade.php +++ b/resources/views/catalog/faculty/index.blade.php @@ -22,8 +22,8 @@ @foreach($faculties as $faculty) {{ $faculty->position }} - {{ $faculty->name }} - {{ $faculty->description }} + {{ $faculty->name }} + {{ Str::words($faculty->description, 10, '...') }} {{ $faculty->educationalInstitution->name }} редактировать diff --git a/resources/views/catalog/faculty/show.blade.php b/resources/views/catalog/faculty/show.blade.php new file mode 100644 index 0000000..c83903a --- /dev/null +++ b/resources/views/catalog/faculty/show.blade.php @@ -0,0 +1,19 @@ +@extends('layouts.admin-layout') +@section('content') + + @auth() +
{{ $faculty->educationalInstitution->name }} -> {{ $faculty->name }}
+
+

Название

+

{{ $faculty->name }}

+

Описание

+

{{ $faculty->description }}

+

Позиция

+

{{ $faculty->position }}

+

Кафедры

+ @foreach($faculty->departments as $department) +

{{ $department->name }}

+ @endforeach +
+ @endauth +@endsection diff --git a/resources/views/layouts/admin-layout.blade.php b/resources/views/layouts/admin-layout.blade.php index a095cd1..2621ac3 100644 --- a/resources/views/layouts/admin-layout.blade.php +++ b/resources/views/layouts/admin-layout.blade.php @@ -61,9 +61,10 @@
  • Учебные заведения
  • Факультеты
  • +
  • Кафедры
  • -
    @yield('content')
    +
    @yield('content')