From 8b708ab5970e028a3fcf493b779b29e04e7cf0b0 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Tue, 7 May 2024 15:19:07 +0400 Subject: [PATCH 01/16] Added score's migration --- ...07_111420_create_student_subject_table.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 database/migrations/2024_05_07_111420_create_student_subject_table.php diff --git a/database/migrations/2024_05_07_111420_create_student_subject_table.php b/database/migrations/2024_05_07_111420_create_student_subject_table.php new file mode 100644 index 0000000..662294e --- /dev/null +++ b/database/migrations/2024_05_07_111420_create_student_subject_table.php @@ -0,0 +1,29 @@ +foreignId('student_id')->constrained('students')->onDelete('cascade'); + $table->foreignId('subject_id')->constrained('subjects')->onDelete('cascade'); + $table->unsignedInteger('score'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('student_subject'); + } +}; From c632489cf319e430010c84febaebb7dc12b7da63 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Tue, 7 May 2024 17:15:58 +0400 Subject: [PATCH 02/16] added Pivot model --- app/Http/Requests/ScorePostRequest.php | 28 ++++++++++++++++++++++++++ app/Models/Score.php | 11 ++++++++++ app/Models/Student.php | 6 ++++++ app/Models/Subject.php | 5 +++++ 4 files changed, 50 insertions(+) create mode 100644 app/Http/Requests/ScorePostRequest.php create mode 100644 app/Models/Score.php diff --git a/app/Http/Requests/ScorePostRequest.php b/app/Http/Requests/ScorePostRequest.php new file mode 100644 index 0000000..d5d9e13 --- /dev/null +++ b/app/Http/Requests/ScorePostRequest.php @@ -0,0 +1,28 @@ +|string> + */ + public function rules(): array + { + return [ + // + ]; + } +} diff --git a/app/Models/Score.php b/app/Models/Score.php new file mode 100644 index 0000000..9c832e0 --- /dev/null +++ b/app/Models/Score.php @@ -0,0 +1,11 @@ +morphOne(User::class, 'userable'); } + public function subjects(): BelongsToMany + { + return $this->belongsToMany(Subject::class)->using(Score::class); + } + public function scopeFilter(Builder $query): void { $name = request('name'); diff --git a/app/Models/Subject.php b/app/Models/Subject.php index 9e92711..7b7cbf2 100644 --- a/app/Models/Subject.php +++ b/app/Models/Subject.php @@ -26,6 +26,11 @@ class Subject extends Model return $this->belongsToMany(Teacher::class); } + public function students(): BelongsToMany + { + return $this->belongsToMany(Student::class)->using(Score::class); + } + public function scopeFilter(Builder $query): void { $name = request('name'); From 4cdbe1ab92d9a6f2c074bbae114b27770ee3eb01 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Tue, 7 May 2024 17:16:34 +0400 Subject: [PATCH 03/16] added Lesson model --- app/Models/Lesson.php | 17 +++++++++++ ...07_111420_create_student_subject_table.php | 3 +- ...2024_05_07_123620_create_lessons_table.php | 30 +++++++++++++++++++ ...455_add_foreign_key_to_student_subject.php | 28 +++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 app/Models/Lesson.php create mode 100644 database/migrations/2024_05_07_123620_create_lessons_table.php create mode 100644 database/migrations/2024_05_07_125455_add_foreign_key_to_student_subject.php diff --git a/app/Models/Lesson.php b/app/Models/Lesson.php new file mode 100644 index 0000000..7506978 --- /dev/null +++ b/app/Models/Lesson.php @@ -0,0 +1,17 @@ +id(); $table->foreignId('student_id')->constrained('students')->onDelete('cascade'); $table->foreignId('subject_id')->constrained('subjects')->onDelete('cascade'); - $table->unsignedInteger('score'); + $table->unsignedInteger('mark'); $table->timestamps(); }); } diff --git a/database/migrations/2024_05_07_123620_create_lessons_table.php b/database/migrations/2024_05_07_123620_create_lessons_table.php new file mode 100644 index 0000000..7e3a76e --- /dev/null +++ b/database/migrations/2024_05_07_123620_create_lessons_table.php @@ -0,0 +1,30 @@ +id(); + $table->string('name'); + $table->string('type'); + $table->date('lesson_date')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('lessons'); + } +}; diff --git a/database/migrations/2024_05_07_125455_add_foreign_key_to_student_subject.php b/database/migrations/2024_05_07_125455_add_foreign_key_to_student_subject.php new file mode 100644 index 0000000..11a4b4a --- /dev/null +++ b/database/migrations/2024_05_07_125455_add_foreign_key_to_student_subject.php @@ -0,0 +1,28 @@ +foreignId('lesson_id')->constrained('lessons')->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('student_subject', function (Blueprint $table) { + $table->dropColumn('lesson_id'); + }); + } +}; From 7a80e52d3c50d052b833dedd3db245abf5d158b7 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Wed, 8 May 2024 11:39:35 +0400 Subject: [PATCH 04/16] relationship between lesson and score --- app/Models/Lesson.php | 6 ++++++ app/Models/Score.php | 6 ++++++ app/Models/Student.php | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/Models/Lesson.php b/app/Models/Lesson.php index 7506978..e05e22a 100644 --- a/app/Models/Lesson.php +++ b/app/Models/Lesson.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasMany; class Lesson extends Model { @@ -14,4 +15,9 @@ class Lesson extends Model 'type', 'lesson_date', ]; + + public function scores(): HasMany + { + return $this->hasMany(Score::class); + } } diff --git a/app/Models/Score.php b/app/Models/Score.php index 9c832e0..3afe486 100644 --- a/app/Models/Score.php +++ b/app/Models/Score.php @@ -3,9 +3,15 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\Pivot; class Score extends Pivot { use HasFactory; + + public function lesson(): BelongsTo + { + return $this->belongsTo(Lesson::class); + } } diff --git a/app/Models/Student.php b/app/Models/Student.php index a08ff29..6628573 100644 --- a/app/Models/Student.php +++ b/app/Models/Student.php @@ -34,7 +34,7 @@ class Student extends Model public function subjects(): BelongsToMany { - return $this->belongsToMany(Subject::class)->using(Score::class); + return $this->belongsToMany(Subject::class)->withPivot('mark')->using(Score::class); } public function scopeFilter(Builder $query): void From 1a0cf485150c4ddba39ecb26e8f92f4511d99755 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Wed, 8 May 2024 12:03:55 +0400 Subject: [PATCH 05/16] changed migrations --- .../migrations/2024_05_07_123620_create_lessons_table.php | 5 ++++- .../2024_05_07_125455_add_foreign_key_to_student_subject.php | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/database/migrations/2024_05_07_123620_create_lessons_table.php b/database/migrations/2024_05_07_123620_create_lessons_table.php index 7e3a76e..62797e7 100644 --- a/database/migrations/2024_05_07_123620_create_lessons_table.php +++ b/database/migrations/2024_05_07_123620_create_lessons_table.php @@ -15,7 +15,10 @@ return new class extends Migration $table->id(); $table->string('name'); $table->string('type'); - $table->date('lesson_date')->nullable(); + $table->date('lesson_date'); + $table->foreignId('grade_id')->constrained('grades')->onDelete('cascade'); + $table->foreignId('subject_id')->constrained('subjects')->onDelete('cascade'); + $table->foreignId('teacher_id')->constrained('teachers')->onDelete('cascade'); $table->timestamps(); }); } diff --git a/database/migrations/2024_05_07_125455_add_foreign_key_to_student_subject.php b/database/migrations/2024_05_07_125455_add_foreign_key_to_student_subject.php index 11a4b4a..3e177d1 100644 --- a/database/migrations/2024_05_07_125455_add_foreign_key_to_student_subject.php +++ b/database/migrations/2024_05_07_125455_add_foreign_key_to_student_subject.php @@ -12,7 +12,7 @@ return new class extends Migration public function up(): void { Schema::table('student_subject', function (Blueprint $table) { - $table->foreignId('lesson_id')->constrained('lessons')->onDelete('cascade'); + $table->foreignId('lesson_id')->nullable()->constrained('lessons')->onDelete('cascade'); }); } From 08f5909a9f4eb2e0ff6639a00bae7a51af5101e5 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Wed, 8 May 2024 12:15:55 +0400 Subject: [PATCH 06/16] changed models --- app/Models/Grade.php | 5 +++++ app/Models/Lesson.php | 16 ++++++++++++++++ app/Models/Subject.php | 6 ++++++ app/Models/Teacher.php | 6 ++++++ 4 files changed, 33 insertions(+) diff --git a/app/Models/Grade.php b/app/Models/Grade.php index 04d734e..2d9d76c 100644 --- a/app/Models/Grade.php +++ b/app/Models/Grade.php @@ -31,6 +31,11 @@ class Grade extends Model return $this->belongsToMany(Teacher::class); } + public function lessons(): HasMany + { + return $this->hasMany(Lesson::class); + } + public function scopeFilter(Builder $query): void { $name = request('name'); diff --git a/app/Models/Lesson.php b/app/Models/Lesson.php index e05e22a..cfef020 100644 --- a/app/Models/Lesson.php +++ b/app/Models/Lesson.php @@ -4,6 +4,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 Lesson extends Model @@ -20,4 +21,19 @@ class Lesson extends Model { return $this->hasMany(Score::class); } + + public function grades(): BelongsTo + { + return $this->belongsTo(Grade::class); + } + + public function teachers(): BelongsTo + { + return $this->belongsTo(Teacher::class); + } + + public function subjects(): BelongsTo + { + return $this->belongsTo(Subject::class); + } } diff --git a/app/Models/Subject.php b/app/Models/Subject.php index 7b7cbf2..ef256ed 100644 --- a/app/Models/Subject.php +++ b/app/Models/Subject.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphOne; class Subject extends Model @@ -31,6 +32,11 @@ class Subject extends Model return $this->belongsToMany(Student::class)->using(Score::class); } + public function lessons(): HasMany + { + return $this->hasMany(Lesson::class); + } + public function scopeFilter(Builder $query): void { $name = request('name'); diff --git a/app/Models/Teacher.php b/app/Models/Teacher.php index f18c015..0ce1e37 100644 --- a/app/Models/Teacher.php +++ b/app/Models/Teacher.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphOne; class Teacher extends Model @@ -35,6 +36,11 @@ class Teacher extends Model return $this->morphOne(User::class, 'userable'); } + public function lessons(): HasMany + { + return $this->hasMany(Lesson::class); + } + public function scopeFilter(Builder $query): void { $name = request('name'); From 85d7aacb0222d3a3c57a8cb27637f961c7b674ac Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Wed, 8 May 2024 12:59:29 +0400 Subject: [PATCH 07/16] added controller and service --- app/Http/Controllers/LessonController.php | 88 +++++++++++++++++++++++ app/Http/Requests/LessonPostRequest.php | 35 +++++++++ app/Models/Lesson.php | 3 + app/Providers/ModelServiceProvider.php | 8 +++ app/Services/LessonService.php | 33 +++++++++ 5 files changed, 167 insertions(+) create mode 100644 app/Http/Controllers/LessonController.php create mode 100644 app/Http/Requests/LessonPostRequest.php create mode 100644 app/Services/LessonService.php diff --git a/app/Http/Controllers/LessonController.php b/app/Http/Controllers/LessonController.php new file mode 100644 index 0000000..665d07d --- /dev/null +++ b/app/Http/Controllers/LessonController.php @@ -0,0 +1,88 @@ + $this->service->getAll(), + ]); + } + + /** + * Show the form for creating a new resource. + */ + public function create(): View + { + return view('lessons.create'); + } + + /** + * Store a newly created resource in storage. + */ + public function store(LessonPostRequest $request): RedirectResponse + { + return redirect()->route( + 'lessons.show', + $this->service->create($request->validated()) + ); + } + + /** + * Display the specified resource. + */ + public function show(Lesson $lesson): View + { + return view('lessons.show', [ + 'lesson' => $lesson, + ]); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(Lesson $lesson): View + { + return view('lessons.edit', [ + 'lesson' => $lesson, + ]); + } + + /** + * Update the specified resource in storage. + */ + public function update(LessonPostRequest $request, Lesson $lesson): RedirectResponse + { + return redirect()->route( + 'lessons.show', + $this->service->update($lesson, $request->validated()) + ); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(Lesson $lesson): RedirectResponse + { + $this->service->delete($lesson); + + return redirect()->route('lessons.index'); + } +} diff --git a/app/Http/Requests/LessonPostRequest.php b/app/Http/Requests/LessonPostRequest.php new file mode 100644 index 0000000..930a26d --- /dev/null +++ b/app/Http/Requests/LessonPostRequest.php @@ -0,0 +1,35 @@ +|string> + */ + public function rules(): array + { + return [ + 'name' => 'required|string|max:255', + 'type' => ['required', Rule::in(TypeLesson::cases())], + 'lesson_date' => 'required|date', + 'grade_id' => 'required|exists:grades,id', + 'teacher_id' => 'required|exists:teachers,id', + 'subject_id' => 'required|exists:subjects,id', + ]; + } +} diff --git a/app/Models/Lesson.php b/app/Models/Lesson.php index cfef020..045cc87 100644 --- a/app/Models/Lesson.php +++ b/app/Models/Lesson.php @@ -15,6 +15,9 @@ class Lesson extends Model 'name', 'type', 'lesson_date', + 'grade_id', + 'teacher_id', + 'subject_id', ]; public function scores(): HasMany diff --git a/app/Providers/ModelServiceProvider.php b/app/Providers/ModelServiceProvider.php index e7971b8..aea0c78 100644 --- a/app/Providers/ModelServiceProvider.php +++ b/app/Providers/ModelServiceProvider.php @@ -5,6 +5,7 @@ namespace App\Providers; use App\Http\Controllers\GradeController; use App\Http\Controllers\GradeSubjectController; use App\Http\Controllers\GradeTeacherController; +use App\Http\Controllers\LessonController; use App\Http\Controllers\StudentController; use App\Http\Controllers\SubjectController; use App\Http\Controllers\SubjectTeacherController; @@ -12,6 +13,7 @@ use App\Http\Controllers\TeacherController; use App\Services\GradeService; use App\Services\GradeSubjectService; use App\Services\GradeTeacherService; +use App\Services\LessonService; use App\Services\ServiceInterface; use App\Services\StudentService; use App\Services\SubjectService; @@ -67,6 +69,12 @@ class ModelServiceProvider extends ServiceProvider ->give(function () { return new GradeTeacherService(); }); + + $this->app->when(LessonController::class) + ->needs(ServiceInterface::class) + ->give(function () { + return new LessonService(); + }); } /** diff --git a/app/Services/LessonService.php b/app/Services/LessonService.php new file mode 100644 index 0000000..bc5e8fa --- /dev/null +++ b/app/Services/LessonService.php @@ -0,0 +1,33 @@ +update($data); + + return $model; + } + + public function delete($model): void + { + $model->delete(); + } +} From a9ed0e58fd612d5a4dc0ea9cb30d38e32871a926 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Wed, 8 May 2024 12:59:44 +0400 Subject: [PATCH 08/16] added enum --- app/Enums/TypeLesson.php | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 app/Enums/TypeLesson.php diff --git a/app/Enums/TypeLesson.php b/app/Enums/TypeLesson.php new file mode 100644 index 0000000..ebbbe42 --- /dev/null +++ b/app/Enums/TypeLesson.php @@ -0,0 +1,11 @@ + Date: Wed, 8 May 2024 13:34:54 +0400 Subject: [PATCH 09/16] added routes desciption to Lesson --- app/Enums/TypeLesson.php | 2 +- app/Http/Controllers/LessonController.php | 6 +++++- app/Models/Lesson.php | 7 ++++--- .../migrations/2024_05_07_123620_create_lessons_table.php | 1 + routes/web.php | 2 ++ 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/Enums/TypeLesson.php b/app/Enums/TypeLesson.php index ebbbe42..545bcb6 100644 --- a/app/Enums/TypeLesson.php +++ b/app/Enums/TypeLesson.php @@ -4,7 +4,7 @@ namespace App\Enums; enum TypeLesson: string { - case Homework = "Домашнее задание"; + case Homework = "Домашняя работа"; case Classwork = "Работа в классе"; case TestClass = "Самостоятельная работа"; case ExamClass = "Контрольная работа"; diff --git a/app/Http/Controllers/LessonController.php b/app/Http/Controllers/LessonController.php index 665d07d..e0ca795 100644 --- a/app/Http/Controllers/LessonController.php +++ b/app/Http/Controllers/LessonController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Enums\TypeLesson; use App\Http\Requests\LessonPostRequest; use App\Models\Lesson; use App\Services\ServiceInterface; @@ -31,7 +32,9 @@ class LessonController extends Controller */ public function create(): View { - return view('lessons.create'); + return view('lessons.create', [ + 'types' => TypeLesson::cases(), + ]); } /** @@ -62,6 +65,7 @@ class LessonController extends Controller { return view('lessons.edit', [ 'lesson' => $lesson, + 'types' => TypeLesson::cases(), ]); } diff --git a/app/Models/Lesson.php b/app/Models/Lesson.php index 045cc87..a1061b9 100644 --- a/app/Models/Lesson.php +++ b/app/Models/Lesson.php @@ -14,6 +14,7 @@ class Lesson extends Model protected $fillable = [ 'name', 'type', + 'description', 'lesson_date', 'grade_id', 'teacher_id', @@ -25,17 +26,17 @@ class Lesson extends Model return $this->hasMany(Score::class); } - public function grades(): BelongsTo + public function grade(): BelongsTo { return $this->belongsTo(Grade::class); } - public function teachers(): BelongsTo + public function teacher(): BelongsTo { return $this->belongsTo(Teacher::class); } - public function subjects(): BelongsTo + public function subject(): BelongsTo { return $this->belongsTo(Subject::class); } diff --git a/database/migrations/2024_05_07_123620_create_lessons_table.php b/database/migrations/2024_05_07_123620_create_lessons_table.php index 62797e7..d281de2 100644 --- a/database/migrations/2024_05_07_123620_create_lessons_table.php +++ b/database/migrations/2024_05_07_123620_create_lessons_table.php @@ -14,6 +14,7 @@ return new class extends Migration Schema::create('lessons', function (Blueprint $table) { $table->id(); $table->string('name'); + $table->string('description')->nullable(); $table->string('type'); $table->date('lesson_date'); $table->foreignId('grade_id')->constrained('grades')->onDelete('cascade'); diff --git a/routes/web.php b/routes/web.php index 59933d2..a8c97ba 100644 --- a/routes/web.php +++ b/routes/web.php @@ -3,6 +3,7 @@ use App\Http\Controllers\GradeController; use App\Http\Controllers\GradeSubjectController; use App\Http\Controllers\GradeTeacherController; +use App\Http\Controllers\LessonController; use App\Http\Controllers\StudentController; use App\Http\Controllers\SubjectController; use App\Http\Controllers\SubjectTeacherController; @@ -18,6 +19,7 @@ Route::resources([ 'subjects' => SubjectController::class, 'students' => StudentController::class, 'teachers' => TeacherController::class, + 'lessons' => LessonController::class, ]); Route::resource('teachers.subjects', SubjectTeacherController::class)->except('index'); From 6ad697924ac0b7ce37d86687e2b81f846b08f9d1 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Wed, 8 May 2024 16:55:32 +0400 Subject: [PATCH 10/16] Added lesson's views --- app/Http/Controllers/LessonController.php | 50 +++++++++++------- app/Http/Requests/LessonPostRequest.php | 1 + app/Services/LessonService.php | 10 +++- resources/views/grade-lesson/create.blade.php | 5 ++ resources/views/grade-lesson/edit.blade.php | 5 ++ resources/views/grade-lesson/form.blade.php | 51 +++++++++++++++++++ .../views/grade-lesson/grades-list.blade.php | 22 ++++++++ resources/views/grade-lesson/index.blade.php | 35 +++++++++++++ resources/views/grade-lesson/show.blade.php | 30 +++++++++++ resources/views/includes/footer.blade.php | 1 + resources/views/includes/header.blade.php | 3 +- routes/web.php | 3 +- 12 files changed, 195 insertions(+), 21 deletions(-) create mode 100644 resources/views/grade-lesson/create.blade.php create mode 100644 resources/views/grade-lesson/edit.blade.php create mode 100644 resources/views/grade-lesson/form.blade.php create mode 100644 resources/views/grade-lesson/grades-list.blade.php create mode 100644 resources/views/grade-lesson/index.blade.php create mode 100644 resources/views/grade-lesson/show.blade.php diff --git a/app/Http/Controllers/LessonController.php b/app/Http/Controllers/LessonController.php index e0ca795..1c829c5 100644 --- a/app/Http/Controllers/LessonController.php +++ b/app/Http/Controllers/LessonController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use App\Enums\TypeLesson; use App\Http\Requests\LessonPostRequest; +use App\Models\Grade; use App\Models\Lesson; use App\Services\ServiceInterface; use Illuminate\Http\RedirectResponse; @@ -17,54 +18,67 @@ class LessonController extends Controller ){ } + public function gradeList(): View + { + return view('grade-lesson.grades-list', [ + 'grades' => $this->service->getGrades(), + ]); + } + /** * Display a listing of the resource. */ - public function index(): View + public function index(Grade $grade): View { - return view('lessons.index', [ - 'lessons' => $this->service->getAll(), + return view('grade-lesson.index', [ + 'lessons' => $this->service->getAll($grade), + 'grade' => $grade, ]); } /** * Show the form for creating a new resource. */ - public function create(): View + public function create(Grade $grade): View { - return view('lessons.create', [ + return view('grade-lesson.create', [ 'types' => TypeLesson::cases(), + 'grade' => $grade, ]); } /** * Store a newly created resource in storage. */ - public function store(LessonPostRequest $request): RedirectResponse + public function store(LessonPostRequest $request, Grade $grade): RedirectResponse { return redirect()->route( - 'lessons.show', - $this->service->create($request->validated()) + 'grades.lessons.show', [ + $grade, + $this->service->create($request->validated()) + ] ); } /** * Display the specified resource. */ - public function show(Lesson $lesson): View + public function show(Grade $grade, Lesson $lesson): View { - return view('lessons.show', [ + return view('grade-lesson.show', [ 'lesson' => $lesson, + 'grade' => $grade, ]); } /** * Show the form for editing the specified resource. */ - public function edit(Lesson $lesson): View + public function edit(Grade $grade, Lesson $lesson): View { - return view('lessons.edit', [ + return view('grade-lesson.edit', [ 'lesson' => $lesson, + 'grade' => $grade, 'types' => TypeLesson::cases(), ]); } @@ -72,21 +86,23 @@ class LessonController extends Controller /** * Update the specified resource in storage. */ - public function update(LessonPostRequest $request, Lesson $lesson): RedirectResponse + public function update(LessonPostRequest $request, Grade $grade, Lesson $lesson): RedirectResponse { return redirect()->route( - 'lessons.show', - $this->service->update($lesson, $request->validated()) + 'grades.lessons.show',[ + $grade, + $this->service->update($lesson, $request->validated()) + ] ); } /** * Remove the specified resource from storage. */ - public function destroy(Lesson $lesson): RedirectResponse + public function destroy(Grade $grade, Lesson $lesson): RedirectResponse { $this->service->delete($lesson); - return redirect()->route('lessons.index'); + return redirect()->route('grades.lessons.index', $grade); } } diff --git a/app/Http/Requests/LessonPostRequest.php b/app/Http/Requests/LessonPostRequest.php index 930a26d..174fe96 100644 --- a/app/Http/Requests/LessonPostRequest.php +++ b/app/Http/Requests/LessonPostRequest.php @@ -27,6 +27,7 @@ class LessonPostRequest extends FormRequest 'name' => 'required|string|max:255', 'type' => ['required', Rule::in(TypeLesson::cases())], 'lesson_date' => 'required|date', + 'description' => 'nullable|string|max:250', 'grade_id' => 'required|exists:grades,id', 'teacher_id' => 'required|exists:teachers,id', 'subject_id' => 'required|exists:subjects,id', diff --git a/app/Services/LessonService.php b/app/Services/LessonService.php index bc5e8fa..1aa7bb4 100644 --- a/app/Services/LessonService.php +++ b/app/Services/LessonService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Models\Grade; use App\Models\Lesson; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; @@ -9,9 +10,14 @@ use Illuminate\Database\Eloquent\Model; class LessonService implements ServiceInterface { - public function getAll(): Collection + public function getAll(?Grade $grade = null): Collection { - return Lesson::all(); + return $grade->lessons; + } + + public function getGrades(): Collection + { + return Grade::all(); } public function create(array $data): Lesson diff --git a/resources/views/grade-lesson/create.blade.php b/resources/views/grade-lesson/create.blade.php new file mode 100644 index 0000000..10d197b --- /dev/null +++ b/resources/views/grade-lesson/create.blade.php @@ -0,0 +1,5 @@ +@extends('layouts.app') + +@section('content') + @include('grade-lesson.form', ['route' => route('grades.lessons.store', $grade), 'method' => 'POST']) +@endsection diff --git a/resources/views/grade-lesson/edit.blade.php b/resources/views/grade-lesson/edit.blade.php new file mode 100644 index 0000000..9866f01 --- /dev/null +++ b/resources/views/grade-lesson/edit.blade.php @@ -0,0 +1,5 @@ +@extends('layouts.app') + +@section('content') + @include('grade-lesson.form', ['route' => route('grades.lessons.update', [$grade, $lesson]), 'method' => 'PUT']) +@endsection diff --git a/resources/views/grade-lesson/form.blade.php b/resources/views/grade-lesson/form.blade.php new file mode 100644 index 0000000..2cb5e79 --- /dev/null +++ b/resources/views/grade-lesson/form.blade.php @@ -0,0 +1,51 @@ +
+
+
+
+
+ @csrf + @method($method) +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ + {{-- заглушка --}} +
+ +
+
+
+
+
+
diff --git a/resources/views/grade-lesson/grades-list.blade.php b/resources/views/grade-lesson/grades-list.blade.php new file mode 100644 index 0000000..3c59b88 --- /dev/null +++ b/resources/views/grade-lesson/grades-list.blade.php @@ -0,0 +1,22 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
{{__('Классы')}}
+
+ @if (count($grades)) +
+ @foreach ($grades as $grade) + {{ $grade->name }} + @endforeach +
+ @else +

Классы отсутствуют

+ @endif +
+
+
+
+@endsection diff --git a/resources/views/grade-lesson/index.blade.php b/resources/views/grade-lesson/index.blade.php new file mode 100644 index 0000000..34617be --- /dev/null +++ b/resources/views/grade-lesson/index.blade.php @@ -0,0 +1,35 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
{{__('Создание занятия')}}
+ +
+ @if(count($lessons)) + @foreach($lessons as $lesson) + +
+
{{$lesson->subject->name}}
+
{{$lesson->type}}
+
+ {{ $lesson->name }} +
+ +
+
+ @endforeach + @endif +
+
+@endsection + diff --git a/resources/views/grade-lesson/show.blade.php b/resources/views/grade-lesson/show.blade.php new file mode 100644 index 0000000..55a6306 --- /dev/null +++ b/resources/views/grade-lesson/show.blade.php @@ -0,0 +1,30 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
{{ $lesson->type }}
+
+
+
Тема: {{ $lesson->name }}
+
+
+
Предмет: {{ $lesson->subject->name }}
+
+
+
Дата: {{ $lesson->lesson_date }}
+
+
+
Описание: {{ $lesson->description }}
+
+
+ +
+
+
+@endsection + + diff --git a/resources/views/includes/footer.blade.php b/resources/views/includes/footer.blade.php index 780e7f8..016be12 100644 --- a/resources/views/includes/footer.blade.php +++ b/resources/views/includes/footer.blade.php @@ -3,6 +3,7 @@ diff --git a/resources/views/includes/header.blade.php b/resources/views/includes/header.blade.php index 9e59fab..31e0472 100644 --- a/resources/views/includes/header.blade.php +++ b/resources/views/includes/header.blade.php @@ -2,7 +2,8 @@
diff --git a/routes/web.php b/routes/web.php index a8c97ba..fd37d1c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -19,9 +19,10 @@ Route::resources([ 'subjects' => SubjectController::class, 'students' => StudentController::class, 'teachers' => TeacherController::class, - 'lessons' => LessonController::class, + 'grades.lessons' => LessonController::class, ]); Route::resource('teachers.subjects', SubjectTeacherController::class)->except('index'); Route::resource('teachers.subjects.grades', GradeTeacherController::class)->except('index', 'show'); Route::resource('grades.subjects', GradeSubjectController::class)->except('index', 'show'); +Route::get('lessons/grade-list', [LessonController::class, 'gradeList'])->name('grades.lessons.grades-list'); From 619254a20467d57dc9f144b9ae5ba73742e470e3 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Mon, 13 May 2024 14:23:21 +0400 Subject: [PATCH 11/16] added filter to lessons --- app/Http/Controllers/LessonController.php | 1 + app/Models/Lesson.php | 10 ++++++++ app/Services/LessonService.php | 2 +- resources/views/grade-lesson/index.blade.php | 24 +++++++++++++++++++- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/LessonController.php b/app/Http/Controllers/LessonController.php index 1c829c5..d33d431 100644 --- a/app/Http/Controllers/LessonController.php +++ b/app/Http/Controllers/LessonController.php @@ -33,6 +33,7 @@ class LessonController extends Controller return view('grade-lesson.index', [ 'lessons' => $this->service->getAll($grade), 'grade' => $grade, + 'subjects' => $grade->subjects, ]); } diff --git a/app/Models/Lesson.php b/app/Models/Lesson.php index a1061b9..3af1d3e 100644 --- a/app/Models/Lesson.php +++ b/app/Models/Lesson.php @@ -2,6 +2,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\BelongsTo; @@ -40,4 +41,13 @@ class Lesson extends Model { return $this->belongsTo(Subject::class); } + + public function scopeFilter(Builder $query): void + { + $subject_id = request('subject_id'); + + $query->when($subject_id, function (Builder $query, $subject_id) { + $query->where('subject_id', $subject_id); + }); + } } diff --git a/app/Services/LessonService.php b/app/Services/LessonService.php index 1aa7bb4..b32a63d 100644 --- a/app/Services/LessonService.php +++ b/app/Services/LessonService.php @@ -12,7 +12,7 @@ class LessonService implements ServiceInterface public function getAll(?Grade $grade = null): Collection { - return $grade->lessons; + return $grade->lessons()->filter()->get(); } public function getGrades(): Collection diff --git a/resources/views/grade-lesson/index.blade.php b/resources/views/grade-lesson/index.blade.php index 34617be..08d752c 100644 --- a/resources/views/grade-lesson/index.blade.php +++ b/resources/views/grade-lesson/index.blade.php @@ -1,4 +1,4 @@ -@extends('layouts.app') +`@extends('layouts.app') @section('content')
@@ -9,6 +9,27 @@ Добавить
+
+
{{__('Фильтрация')}}
+
+
+
+ + +
+
+ +
+
+
+
@if(count($lessons)) @foreach($lessons as $lesson) @@ -33,3 +54,4 @@ @endsection +` From 2de4f3ca82a939745a9ef967f6c2054498731d6a Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Mon, 13 May 2024 14:43:49 +0400 Subject: [PATCH 12/16] Remake many-to-many for scores --- app/Models/Lesson.php | 6 ++++ app/Models/Score.php | 17 ----------- app/Models/Student.php | 7 ++++- ...455_add_foreign_key_to_student_subject.php | 28 ------------------- ...13_102702_create_lesson_student_table.php} | 10 +++---- 5 files changed, 17 insertions(+), 51 deletions(-) delete mode 100644 app/Models/Score.php delete mode 100644 database/migrations/2024_05_07_125455_add_foreign_key_to_student_subject.php rename database/migrations/{2024_05_07_111420_create_student_subject_table.php => 2024_05_13_102702_create_lesson_student_table.php} (55%) diff --git a/app/Models/Lesson.php b/app/Models/Lesson.php index 3af1d3e..d84a598 100644 --- a/app/Models/Lesson.php +++ b/app/Models/Lesson.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; class Lesson extends Model @@ -42,6 +43,11 @@ class Lesson extends Model return $this->belongsTo(Subject::class); } + public function students(): BelongsToMany + { + return $this->belongsToMany(Student::class); + } + public function scopeFilter(Builder $query): void { $subject_id = request('subject_id'); diff --git a/app/Models/Score.php b/app/Models/Score.php deleted file mode 100644 index 3afe486..0000000 --- a/app/Models/Score.php +++ /dev/null @@ -1,17 +0,0 @@ -belongsTo(Lesson::class); - } -} diff --git a/app/Models/Student.php b/app/Models/Student.php index 6628573..dd09607 100644 --- a/app/Models/Student.php +++ b/app/Models/Student.php @@ -34,7 +34,12 @@ class Student extends Model public function subjects(): BelongsToMany { - return $this->belongsToMany(Subject::class)->withPivot('mark')->using(Score::class); + return $this->belongsToMany(Subject::class); + } + + public function lessons(): BelongsToMany + { + return $this->belongsToMany(Lesson::class)->withPivot('score'); } public function scopeFilter(Builder $query): void diff --git a/database/migrations/2024_05_07_125455_add_foreign_key_to_student_subject.php b/database/migrations/2024_05_07_125455_add_foreign_key_to_student_subject.php deleted file mode 100644 index 3e177d1..0000000 --- a/database/migrations/2024_05_07_125455_add_foreign_key_to_student_subject.php +++ /dev/null @@ -1,28 +0,0 @@ -foreignId('lesson_id')->nullable()->constrained('lessons')->onDelete('cascade'); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::table('student_subject', function (Blueprint $table) { - $table->dropColumn('lesson_id'); - }); - } -}; diff --git a/database/migrations/2024_05_07_111420_create_student_subject_table.php b/database/migrations/2024_05_13_102702_create_lesson_student_table.php similarity index 55% rename from database/migrations/2024_05_07_111420_create_student_subject_table.php rename to database/migrations/2024_05_13_102702_create_lesson_student_table.php index 4938b60..1ba5166 100644 --- a/database/migrations/2024_05_07_111420_create_student_subject_table.php +++ b/database/migrations/2024_05_13_102702_create_lesson_student_table.php @@ -11,11 +11,11 @@ return new class extends Migration */ public function up(): void { - Schema::create('student_subject', function (Blueprint $table) { + Schema::create('lesson_student', function (Blueprint $table) { $table->id(); - $table->foreignId('student_id')->constrained('students')->onDelete('cascade'); - $table->foreignId('subject_id')->constrained('subjects')->onDelete('cascade'); - $table->unsignedInteger('mark'); + $table->foreignId('lesson_id')->constrained('lessons')->onDelete('cascade'); + $table->foreignId('student_id')->constrained('lessons')->onDelete('cascade'); + $table->unsignedInteger('score'); $table->timestamps(); }); } @@ -25,6 +25,6 @@ return new class extends Migration */ public function down(): void { - Schema::dropIfExists('student_subject'); + Schema::dropIfExists('lesson_student'); } }; From c269765c39fe9b248200045b46012cb3a503b50f Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Mon, 13 May 2024 16:44:00 +0400 Subject: [PATCH 13/16] changed views --- app/Http/Controllers/ScoreController.php | 10 +++++++ app/Services/ScoreService.php | 30 +++++++++++++++++++ .../views/grade-lesson/grades-list.blade.php | 22 -------------- resources/views/grade-lesson/show.blade.php | 1 + resources/views/grades/index.blade.php | 10 +++---- resources/views/includes/footer.blade.php | 1 - resources/views/includes/header.blade.php | 1 - routes/web.php | 2 +- 8 files changed, 47 insertions(+), 30 deletions(-) create mode 100644 app/Http/Controllers/ScoreController.php create mode 100644 app/Services/ScoreService.php delete mode 100644 resources/views/grade-lesson/grades-list.blade.php diff --git a/app/Http/Controllers/ScoreController.php b/app/Http/Controllers/ScoreController.php new file mode 100644 index 0000000..d4fd3f4 --- /dev/null +++ b/app/Http/Controllers/ScoreController.php @@ -0,0 +1,10 @@ +grade->students; + } + + public function create(array $data): Model + { + + } + + public function update(Model $model, array $data): Model + { + // TODO: Implement update() method. + } + + public function delete(Model $model) + { + // TODO: Implement delete() method. + } +} diff --git a/resources/views/grade-lesson/grades-list.blade.php b/resources/views/grade-lesson/grades-list.blade.php deleted file mode 100644 index 3c59b88..0000000 --- a/resources/views/grade-lesson/grades-list.blade.php +++ /dev/null @@ -1,22 +0,0 @@ -@extends('layouts.app') - -@section('content') -
-
-
-
{{__('Классы')}}
-
- @if (count($grades)) - - @else -

Классы отсутствуют

- @endif -
-
-
-
-@endsection diff --git a/resources/views/grade-lesson/show.blade.php b/resources/views/grade-lesson/show.blade.php index 55a6306..722d92c 100644 --- a/resources/views/grade-lesson/show.blade.php +++ b/resources/views/grade-lesson/show.blade.php @@ -20,6 +20,7 @@ diff --git a/resources/views/grades/index.blade.php b/resources/views/grades/index.blade.php index a2dffb7..8cfb03a 100644 --- a/resources/views/grades/index.blade.php +++ b/resources/views/grades/index.blade.php @@ -36,16 +36,16 @@ class="btn btn-block col-8">{{ $grade->name }} + + + -{{-- --}} -{{--
--}} -{{-- Журнал--}} -{{--
--}} -{{-- --}}
diff --git a/resources/views/includes/footer.blade.php b/resources/views/includes/footer.blade.php index 016be12..780e7f8 100644 --- a/resources/views/includes/footer.blade.php +++ b/resources/views/includes/footer.blade.php @@ -3,7 +3,6 @@ diff --git a/resources/views/includes/header.blade.php b/resources/views/includes/header.blade.php index 31e0472..38aa664 100644 --- a/resources/views/includes/header.blade.php +++ b/resources/views/includes/header.blade.php @@ -3,7 +3,6 @@ diff --git a/routes/web.php b/routes/web.php index fd37d1c..c426a20 100644 --- a/routes/web.php +++ b/routes/web.php @@ -25,4 +25,4 @@ Route::resources([ Route::resource('teachers.subjects', SubjectTeacherController::class)->except('index'); Route::resource('teachers.subjects.grades', GradeTeacherController::class)->except('index', 'show'); Route::resource('grades.subjects', GradeSubjectController::class)->except('index', 'show'); -Route::get('lessons/grade-list', [LessonController::class, 'gradeList'])->name('grades.lessons.grades-list'); +Route::resource('lessons.students', ScoreController::class)->except('show'); From 25d3463182ad9f5897d1b63d6f1747bfc66023c7 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Fri, 17 May 2024 18:46:12 +0400 Subject: [PATCH 14/16] Added show scores + some fixes --- app/Enums/ScoreEnum.php | 14 ++++++ app/Http/Controllers/ScoreController.php | 19 +++++++- app/Models/Lesson.php | 2 +- app/Models/Student.php | 2 +- app/Providers/ModelServiceProvider.php | 9 ++++ resources/views/grade-lesson/show.blade.php | 2 +- resources/views/scores/show.blade.php | 53 +++++++++++++++++++++ resources/views/students/index.blade.php | 2 +- routes/web.php | 4 +- 9 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 app/Enums/ScoreEnum.php create mode 100644 resources/views/scores/show.blade.php diff --git a/app/Enums/ScoreEnum.php b/app/Enums/ScoreEnum.php new file mode 100644 index 0000000..8a4578b --- /dev/null +++ b/app/Enums/ScoreEnum.php @@ -0,0 +1,14 @@ + $this->service->getAll($lesson), + 'lesson' => $lesson, + 'scores' => ScoreEnum::cases(), + ]); + } } diff --git a/app/Models/Lesson.php b/app/Models/Lesson.php index d84a598..a52aad6 100644 --- a/app/Models/Lesson.php +++ b/app/Models/Lesson.php @@ -45,7 +45,7 @@ class Lesson extends Model public function students(): BelongsToMany { - return $this->belongsToMany(Student::class); + return $this->belongsToMany(Student::class)->withPivot('score'); } public function scopeFilter(Builder $query): void diff --git a/app/Models/Student.php b/app/Models/Student.php index dd09607..b7f93d0 100644 --- a/app/Models/Student.php +++ b/app/Models/Student.php @@ -39,7 +39,7 @@ class Student extends Model public function lessons(): BelongsToMany { - return $this->belongsToMany(Lesson::class)->withPivot('score'); + return $this->belongsToMany(Lesson::class); } public function scopeFilter(Builder $query): void diff --git a/app/Providers/ModelServiceProvider.php b/app/Providers/ModelServiceProvider.php index aea0c78..e2a8d11 100644 --- a/app/Providers/ModelServiceProvider.php +++ b/app/Providers/ModelServiceProvider.php @@ -6,6 +6,7 @@ use App\Http\Controllers\GradeController; use App\Http\Controllers\GradeSubjectController; use App\Http\Controllers\GradeTeacherController; use App\Http\Controllers\LessonController; +use App\Http\Controllers\ScoreController; use App\Http\Controllers\StudentController; use App\Http\Controllers\SubjectController; use App\Http\Controllers\SubjectTeacherController; @@ -14,6 +15,7 @@ use App\Services\GradeService; use App\Services\GradeSubjectService; use App\Services\GradeTeacherService; use App\Services\LessonService; +use App\Services\ScoreService; use App\Services\ServiceInterface; use App\Services\StudentService; use App\Services\SubjectService; @@ -75,6 +77,13 @@ class ModelServiceProvider extends ServiceProvider ->give(function () { return new LessonService(); }); + + + $this->app->when(ScoreController::class) + ->needs(ServiceInterface::class) + ->give(function () { + return new ScoreService(); + }); } /** diff --git a/resources/views/grade-lesson/show.blade.php b/resources/views/grade-lesson/show.blade.php index 722d92c..a645a72 100644 --- a/resources/views/grade-lesson/show.blade.php +++ b/resources/views/grade-lesson/show.blade.php @@ -20,7 +20,7 @@ diff --git a/resources/views/scores/show.blade.php b/resources/views/scores/show.blade.php new file mode 100644 index 0000000..c6f8afa --- /dev/null +++ b/resources/views/scores/show.blade.php @@ -0,0 +1,53 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+

{{ $lesson->name }}

+
+
+ + @csrf + @method('PUT') + @if(count($students)) + + + + + + + @foreach ($students as $student) + + + + + @endforeach + +
ФИООценка
+
+

{{ $student->fio }}

+
+
+
+ +
+
+ @else +

Ученики остутствуют

+ @endif + + +
+
+
+
+@endsection diff --git a/resources/views/students/index.blade.php b/resources/views/students/index.blade.php index 822b81a..a3b9cf2 100644 --- a/resources/views/students/index.blade.php +++ b/resources/views/students/index.blade.php @@ -31,7 +31,7 @@ diff --git a/routes/web.php b/routes/web.php index c426a20..99f2059 100644 --- a/routes/web.php +++ b/routes/web.php @@ -4,6 +4,7 @@ use App\Http\Controllers\GradeController; use App\Http\Controllers\GradeSubjectController; use App\Http\Controllers\GradeTeacherController; use App\Http\Controllers\LessonController; +use App\Http\Controllers\ScoreController; use App\Http\Controllers\StudentController; use App\Http\Controllers\SubjectController; use App\Http\Controllers\SubjectTeacherController; @@ -25,4 +26,5 @@ Route::resources([ Route::resource('teachers.subjects', SubjectTeacherController::class)->except('index'); Route::resource('teachers.subjects.grades', GradeTeacherController::class)->except('index', 'show'); Route::resource('grades.subjects', GradeSubjectController::class)->except('index', 'show'); -Route::resource('lessons.students', ScoreController::class)->except('show'); +Route::get('lessons/{lesson}/scores', [ScoreController::class, 'show'])->name('lessons.scores.show'); +Route::put('lessons/{lesson}/scores', [ScoreController::class, 'update'])->name('lessons.scores.update'); From 92efb4498d168270cccb643bd49cb5d908c7c1a1 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Mon, 27 May 2024 16:57:28 +0400 Subject: [PATCH 15/16] Fixed --- app/Enums/ScoreEnum.php | 1 + app/Http/Controllers/ScoreController.php | 8 ++++++++ app/Models/Lesson.php | 5 ----- app/Services/LessonService.php | 8 +++++++- app/Services/ScoreService.php | 20 ++++++++++++------- app/Services/ServiceInterface.php | 4 ++-- ..._13_102702_create_lesson_student_table.php | 5 ++--- resources/views/scores/show.blade.php | 5 ++--- 8 files changed, 35 insertions(+), 21 deletions(-) diff --git a/app/Enums/ScoreEnum.php b/app/Enums/ScoreEnum.php index 8a4578b..fd17bbe 100644 --- a/app/Enums/ScoreEnum.php +++ b/app/Enums/ScoreEnum.php @@ -4,6 +4,7 @@ namespace App\Enums; enum ScoreEnum: string { + case WithoutScore = 'Без оценки'; case One = '1'; case Two = '2'; case Three = '3'; diff --git a/app/Http/Controllers/ScoreController.php b/app/Http/Controllers/ScoreController.php index a60f76c..838ab9b 100644 --- a/app/Http/Controllers/ScoreController.php +++ b/app/Http/Controllers/ScoreController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Enums\ScoreEnum; use App\Models\Lesson; use App\Services\ServiceInterface; +use Illuminate\Http\Request; class ScoreController extends Controller { @@ -22,4 +23,11 @@ class ScoreController extends Controller 'scores' => ScoreEnum::cases(), ]); } + + public function update(Request $request, Lesson $lesson) + { + $this->service->update($lesson, $request->toArray()); + + return redirect()->route('grades.lessons.show', [$lesson->grade, $lesson]); + } } diff --git a/app/Models/Lesson.php b/app/Models/Lesson.php index a52aad6..060f07d 100644 --- a/app/Models/Lesson.php +++ b/app/Models/Lesson.php @@ -23,11 +23,6 @@ class Lesson extends Model 'subject_id', ]; - public function scores(): HasMany - { - return $this->hasMany(Score::class); - } - public function grade(): BelongsTo { return $this->belongsTo(Grade::class); diff --git a/app/Services/LessonService.php b/app/Services/LessonService.php index b32a63d..dc59737 100644 --- a/app/Services/LessonService.php +++ b/app/Services/LessonService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Enums\ScoreEnum; use App\Models\Grade; use App\Models\Lesson; use Illuminate\Database\Eloquent\Collection; @@ -22,7 +23,12 @@ class LessonService implements ServiceInterface public function create(array $data): Lesson { - return Lesson::create($data); + $lesson = Lesson::create($data); + $lesson + ->students() + ->syncWithPivotValues($lesson->grade->students->pluck('id')->all(), ['score' => ScoreEnum::WithoutScore]); + + return $lesson; } public function update(Model $model, array $data): Lesson diff --git a/app/Services/ScoreService.php b/app/Services/ScoreService.php index f0119f3..03d0a90 100644 --- a/app/Services/ScoreService.php +++ b/app/Services/ScoreService.php @@ -10,21 +10,27 @@ class ScoreService implements ServiceInterface public function getAll(?Lesson $lesson = null) { - return $lesson->grade->students; + return $lesson->students; } - public function create(array $data): Model + public function update(Model $model, array $data) { + $model->students->each(function ($item, $key) use ($data, $model) { + if ($data['score' . $item->id]) { + $model->students()->syncWithoutDetaching([$item->id => ['score' => $data['score' . $item->id]]]); + } + }); - } - - public function update(Model $model, array $data): Model - { - // TODO: Implement update() method. + return $model; } public function delete(Model $model) { // TODO: Implement delete() method. } + + public function create(array $data) + { + // TODO: Implement create() method. + } } diff --git a/app/Services/ServiceInterface.php b/app/Services/ServiceInterface.php index ef9a45c..d4b6d33 100644 --- a/app/Services/ServiceInterface.php +++ b/app/Services/ServiceInterface.php @@ -8,9 +8,9 @@ interface ServiceInterface { public function getAll(); - public function create(array $data): Model; + public function create(array $data); - public function update(Model $model, array $data): Model; + public function update(Model $model, array $data); public function delete(Model $model); } diff --git a/database/migrations/2024_05_13_102702_create_lesson_student_table.php b/database/migrations/2024_05_13_102702_create_lesson_student_table.php index 1ba5166..a264a23 100644 --- a/database/migrations/2024_05_13_102702_create_lesson_student_table.php +++ b/database/migrations/2024_05_13_102702_create_lesson_student_table.php @@ -12,10 +12,9 @@ return new class extends Migration public function up(): void { Schema::create('lesson_student', function (Blueprint $table) { - $table->id(); $table->foreignId('lesson_id')->constrained('lessons')->onDelete('cascade'); - $table->foreignId('student_id')->constrained('lessons')->onDelete('cascade'); - $table->unsignedInteger('score'); + $table->foreignId('student_id')->constrained('students')->onDelete('cascade'); + $table->string('score'); $table->timestamps(); }); } diff --git a/resources/views/scores/show.blade.php b/resources/views/scores/show.blade.php index c6f8afa..dad84cb 100644 --- a/resources/views/scores/show.blade.php +++ b/resources/views/scores/show.blade.php @@ -27,10 +27,9 @@
- @foreach($scores as $score) - @endforeach From 65c47fe15157f11bc04103b81ed194fa33d2b949 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Mon, 27 May 2024 18:40:22 +0400 Subject: [PATCH 16/16] Fixed --- app/Services/StudentService.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Services/StudentService.php b/app/Services/StudentService.php index 6aafb74..34d9ae4 100644 --- a/app/Services/StudentService.php +++ b/app/Services/StudentService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Enums\ScoreEnum; use App\Models\Student; use App\Models\User; use Illuminate\Pagination\LengthAwarePaginator; @@ -27,6 +28,9 @@ class StudentService implements ServiceInterface 'grade_id' => $data['grade_id'], ]); $student->user()->save($user); + $student + ->lessons() + ->syncWithPivotValues($student->grade->lessons->pluck('id')->all(), ['score' => ScoreEnum::WithoutScore]); return $student; }