From 2de4f3ca82a939745a9ef967f6c2054498731d6a Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Mon, 13 May 2024 14:43:49 +0400 Subject: [PATCH] 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'); } };