prod #7
@ -4,14 +4,16 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\GradePostRequest;
|
||||
use App\Models\Grade;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class GradeController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
public function index(): View
|
||||
{
|
||||
return view('grades.index', [
|
||||
'grades' => Grade::filter()->paginate(10)->withQueryString(),
|
||||
@ -21,7 +23,7 @@ class GradeController extends Controller
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
public function create(): View
|
||||
{
|
||||
return view('grades.create');
|
||||
}
|
||||
@ -29,7 +31,7 @@ class GradeController extends Controller
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(GradePostRequest $request)
|
||||
public function store(GradePostRequest $request): RedirectResponse
|
||||
{
|
||||
$grade = Grade::create($request->validated());
|
||||
|
||||
@ -39,7 +41,7 @@ class GradeController extends Controller
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Grade $grade)
|
||||
public function show(Grade $grade): View
|
||||
{
|
||||
return view('grades.show', [
|
||||
'grade' => $grade,
|
||||
@ -49,7 +51,7 @@ class GradeController extends Controller
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(Grade $grade)
|
||||
public function edit(Grade $grade): View
|
||||
{
|
||||
return view('grades.edit', [
|
||||
'grade' => $grade,
|
||||
@ -59,7 +61,7 @@ class GradeController extends Controller
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(GradePostRequest $request, Grade $grade)
|
||||
public function update(GradePostRequest $request, Grade $grade): RedirectResponse
|
||||
{
|
||||
$grade->update($request->validated());
|
||||
|
||||
@ -69,7 +71,7 @@ class GradeController extends Controller
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(Grade $grade)
|
||||
public function destroy(Grade $grade): RedirectResponse
|
||||
{
|
||||
$grade->delete();
|
||||
|
||||
|
80
app/Http/Controllers/SubjectController.php
Normal file
80
app/Http/Controllers/SubjectController.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\SubjectPostRequest;
|
||||
use App\Models\Subject;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class SubjectController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(): View
|
||||
{
|
||||
return view('subjects.index', [
|
||||
"subjects" => Subject::filter()->paginate(10)->withQueryString(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
return view('subjects.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(SubjectPostRequest $request): RedirectResponse
|
||||
{
|
||||
$subject = Subject::create($request->validated());
|
||||
|
||||
return redirect()->route('subjects.show', $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Subject $subject): View
|
||||
{
|
||||
return view('subjects.show', [
|
||||
"subject" => $subject
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(Subject $subject): View
|
||||
{
|
||||
return view('subjects.edit', [
|
||||
'subject' => $subject,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(SubjectPostRequest $request, Subject $subject): RedirectResponse
|
||||
{
|
||||
$subject->update($request->validated());
|
||||
|
||||
return redirect()->route('subjects.show', $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(Subject $subject): RedirectResponse
|
||||
{
|
||||
$subject->delete();
|
||||
|
||||
return redirect()->route('subjects.index');
|
||||
}
|
||||
}
|
28
app/Http/Requests/SubjectPostRequest.php
Normal file
28
app/Http/Requests/SubjectPostRequest.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class SubjectPostRequest 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, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required|unique:grades|max:255',
|
||||
];
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
class Grade extends Model
|
||||
{
|
||||
@ -14,7 +15,12 @@ class Grade extends Model
|
||||
'name',
|
||||
];
|
||||
|
||||
public function scopeFilter(Builder $query)
|
||||
public function subjects(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Subject::class);
|
||||
}
|
||||
|
||||
public function scopeFilter(Builder $query): void
|
||||
{
|
||||
$name = request('name');
|
||||
|
||||
|
31
app/Models/Subject.php
Normal file
31
app/Models/Subject.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
class Subject extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
];
|
||||
|
||||
public function grades(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Grade::class);
|
||||
}
|
||||
|
||||
public function scopeFilter(Builder $query): void
|
||||
{
|
||||
$name = request('name');
|
||||
|
||||
$query->when($name, function (Builder $query, $name) {
|
||||
$query->whereRaw('name ilike ?', ["$name%"]);
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
<?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('subjects', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('subjects');
|
||||
}
|
||||
};
|
@ -0,0 +1,28 @@
|
||||
<?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('grade_subject', function (Blueprint $table) {
|
||||
$table->foreignId('grade_id')->constrained('grades')->onDelete('cascade');
|
||||
$table->foreignId('subject_id')->constrained('subjects')->onDelete('cascade');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('grade_subject');
|
||||
}
|
||||
};
|
@ -2,7 +2,7 @@
|
||||
<footer class="footer py-3 my-4">
|
||||
<ul class="nav justify-content-center pb-3 mb-3">
|
||||
<li class="nav-item"><a href="{{ route('grades.index') }}" class="nav-link px-2 text-muted">Группы</a></li>
|
||||
{{-- <li class="nav-item"><a href="{{ route('subjects.index') }}" class="nav-link px-2 text-muted">Предметы</a></li>--}}
|
||||
<li class="nav-item"><a href="{{ route('subjects.index') }}" class="nav-link px-2 text-muted">Предметы</a></li>
|
||||
{{-- <li class="nav-item"><a href="{{ route('users.index') }}" class="nav-link px-2 text-muted">Студенты</a></li>--}}
|
||||
</ul>
|
||||
<p class="text-center text-muted">© 2024 EduDiary</p>
|
||||
|
@ -2,7 +2,7 @@
|
||||
<header class="d-flex justify-content-center py-3">
|
||||
<ul class="nav nav-pills">
|
||||
<li class="nav-item"><a href="{{ route('grades.index') }}" class="nav-link @if(request()->is('grades*')) active @endif" aria-current="page">Группы</a></li>
|
||||
{{-- <li class="nav-item"><a href="{{ route('subjects.index') }}" class="nav-link @if(request()->is('subjects*')) active @endif">Предметы</a></li>--}}
|
||||
<li class="nav-item"><a href="{{ route('subjects.index') }}" class="nav-link @if(request()->is('subjects*')) active @endif">Предметы</a></li>
|
||||
{{-- <li class="nav-item"><a href="{{ route('users.index') }}" class="nav-link @if(request()->is('users*')) active @endif">Студенты</a></li>--}}
|
||||
</ul>
|
||||
</header>
|
||||
|
5
resources/views/subjects/create.blade.php
Normal file
5
resources/views/subjects/create.blade.php
Normal file
@ -0,0 +1,5 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
@include('subjects.form', ['route' => route('subjects.store'), 'method' => 'POST'])
|
||||
@endsection
|
5
resources/views/subjects/edit.blade.php
Normal file
5
resources/views/subjects/edit.blade.php
Normal file
@ -0,0 +1,5 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
@include('subjects.form', ['route' => route('subjects.update', $subject), 'method' => 'PUT'])
|
||||
@endsection
|
19
resources/views/subjects/form.blade.php
Normal file
19
resources/views/subjects/form.blade.php
Normal file
@ -0,0 +1,19 @@
|
||||
<div class="container col-md-5">
|
||||
<div class="row justify-content-center">
|
||||
<div class="card mt-4">
|
||||
<div class="card-body">
|
||||
<form action="{{ $route }}" method="POST">
|
||||
@csrf
|
||||
@method($method)
|
||||
<div class="mb-3">
|
||||
<label for="name" class="form-label">Название:</label>
|
||||
<input type="text" id="name" name="name" class="form-control" value="{{ isset($subject) ? $subject->name : '' }}" required>
|
||||
</div>
|
||||
<div>
|
||||
<button type="submit" class="btn btn-success">Подтвердить</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
65
resources/views/subjects/index.blade.php
Normal file
65
resources/views/subjects/index.blade.php
Normal file
@ -0,0 +1,65 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="container col-md-6">
|
||||
<div class="row justify-content-center">
|
||||
<div class="card">
|
||||
<div class="card-header">{{__('Новый предмет')}}</div>
|
||||
<div class="card-body">
|
||||
<a href="{{ route('subjects.create') }}" class="btn btn-success">Добавить</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card mt-4">
|
||||
<div class="card-header">{{__('Поиск предмета')}}</div>
|
||||
<div class="card-body">
|
||||
<form action="{{ route('subjects.index') }}" method="GET">
|
||||
<div class="mb-3">
|
||||
<label for="name" class="form-label">Название:</label>
|
||||
<input type="text" id="name" name="name" class="form-control" value="{{ request('name') ?? '' }}">
|
||||
</div>
|
||||
<div>
|
||||
<button type="submit" class="btn btn-primary">Фильтр</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
@if (count($subjects))
|
||||
<div class="card mt-4">
|
||||
<div class="card-header">{{__('Предметы')}}</div>
|
||||
<div class="card-body">
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<th>Название</th>
|
||||
<th> </th>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach ($subjects as $subject)
|
||||
<tr>
|
||||
<td class="table-text">
|
||||
<div>
|
||||
<a href="{{ route('subjects.show', $subject) }}" class="btn btn-block col-8">{{ $subject->name }}</a>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<div>
|
||||
<a href="{{ route('subjects.edit', $subject) }}" class="btn btn-warning">Редактировать</a>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<form action="{{ route('subjects.destroy', $subject) }}" method="POST" style="display: inline-block;">
|
||||
@csrf
|
||||
@method('DELETE')
|
||||
<button type="submit" class="btn btn-danger">Удалить</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="card-footer">{{ $subjects->links() }}</div>
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
19
resources/views/subjects/show.blade.php
Normal file
19
resources/views/subjects/show.blade.php
Normal file
@ -0,0 +1,19 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="row justify-content-center">
|
||||
<div class="card mt-4 col-4">
|
||||
<div class="card-header" {{ __('Предмет') }}>
|
||||
<div class="card-body">
|
||||
<div class="mb-3">
|
||||
<h4><strong>Название:</strong> {{ $subject->name }}</h4>
|
||||
</div>
|
||||
<div>
|
||||
<a href="{{ route('subjects.edit', $subject) }}" class="btn btn-warning">Редактировать</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\GradeController;
|
||||
use App\Http\Controllers\SubjectController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::get('/', function () {
|
||||
@ -8,3 +9,4 @@ Route::get('/', function () {
|
||||
});
|
||||
|
||||
Route::resource('grades', GradeController::class);
|
||||
Route::resource('subjects', SubjectController::class);
|
||||
|
Loading…
Reference in New Issue
Block a user