prod #7
@ -4,6 +4,7 @@ namespace App\Enums;
|
||||
|
||||
enum ScoreEnum: string
|
||||
{
|
||||
case WithoutScore = 'Без оценки';
|
||||
case One = '1';
|
||||
case Two = '2';
|
||||
case Three = '3';
|
||||
|
@ -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]);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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.
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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();
|
||||
});
|
||||
}
|
||||
|
@ -27,10 +27,9 @@
|
||||
</td>
|
||||
<td>
|
||||
<div>
|
||||
<select id="score" name="score" class="form-select form-select-sm" required>
|
||||
<option value="">Нет оценки</option>
|
||||
<select id="score" name="score{{ $student->id }}" class="form-select form-select-sm">
|
||||
@foreach($scores as $score)
|
||||
<option value="{{ $score }}" {{ isset($student->pivot->score) && $student->pivot->score == $score ? 'selected' : '' }}>
|
||||
<option value="{{ $score }}" {{ isset($student->pivot->score) && $student->pivot->score == $score->value ? 'selected' : '' }}>
|
||||
{{ $score }}
|
||||
</option>
|
||||
@endforeach
|
||||
|
Loading…
Reference in New Issue
Block a user