refactoring Direction edit, add firstOrNew in add entrance-examination
Tests & Lint & Deploy to Railway / build (2.6.6, 20.x, 8.3) (push) Failing after 1m58s Details
Tests & Lint & Deploy to Railway / deploy (push) Has been skipped Details

This commit is contained in:
aslan 2024-03-04 08:56:42 +03:00
parent 44fdacb9e4
commit 86597ad075
4 changed files with 220 additions and 90 deletions

View File

@ -110,6 +110,7 @@ class DirectionController extends Controller
$magistracy = $direction $magistracy = $direction
->entranceExaminations ->entranceExaminations
->where('examination_type_id', '=', '3') ->where('examination_type_id', '=', '3')
->sortBy('position')
->pluck('scores', 'subject_id'); ->pluck('scores', 'subject_id');
return view( return view(
@ -152,7 +153,6 @@ class DirectionController extends Controller
public function update(UpdateDirectionRequest $request, Direction $direction): RedirectResponse public function update(UpdateDirectionRequest $request, Direction $direction): RedirectResponse
{ {
$validated = $request->validated(); $validated = $request->validated();
$direction->name = $validated['name']; $direction->name = $validated['name'];
$direction->full_name = "{$validated['code']} {$validated['name']}"; $direction->full_name = "{$validated['code']} {$validated['name']}";
$direction->description = $validated['description']; $direction->description = $validated['description'];
@ -162,8 +162,39 @@ class DirectionController extends Controller
$direction->education_level_id = $validated['education_level_id']; $direction->education_level_id = $validated['education_level_id'];
$direction->education_form_id = $validated['education_form_id']; $direction->education_form_id = $validated['education_form_id'];
$direction->department_id = $validated['department_id']; $direction->department_id = $validated['department_id'];
$direction->budget_places = $validated['budget_places'];
$direction->quota = $validated['quota'];
$direction->paid_places = $validated['paid_places'];
$direction->cost_paid_place = $validated['cost_paid_place'];
$direction->period = $validated['period'];
$direction->save(); $direction->save();
if (array_key_exists('entrance-examination', $validated)) {
foreach ($validated['entrance-examination'] as $id => $data) {
$entranceExamination = EntranceExamination::firstOrNew(['id' => $id]);
$entranceExamination->direction_id = $direction->id;
$entranceExamination->subject_id = $data['subject_id'];
$entranceExamination->scores = $data['scores'];
$entranceExamination->position = $data['position'];
$entranceExamination->subject_type_id = $data['subject_type_id'];
$entranceExamination->examination_type_id = $data['examination_type_id'];
$entranceExamination->save();
}
}
// dd($validated);
if (array_key_exists('delete', $validated)) {
foreach ($validated['delete'] as $id => $value) {
$entranceExamination = EntranceExamination::firstOrNew(['id' => $id]);
$entranceExamination->delete();
}
}
if (array_key_exists('direction_profiles', $validated)) {
$direction->directionProfiles()->sync($validated['direction_profiles']);
}
return redirect()->route('directions.index'); return redirect()->route('directions.index');
} }

View File

@ -15,24 +15,26 @@ class UpdateDirectionRequest extends FormRequest
public function rules(): array public function rules(): array
{ {
return [ return [
'position' => 'required||numeric|int|max:255', 'position' => 'required|int|numeric|max:255',
'description' => 'string',
'department_id' => 'int|required|numeric|max:1000',
'slug' => [
'required',
'string',
'max:255',
"unique:directions,slug,{$this->direction->id}",
],
'name' => 'required|string|max:255', 'name' => 'required|string|max:255',
'description' => 'string',
'slug' => "required|string|max:255|unique:directions,slug,{$this->direction->id}",
'code' => "required|string|max:255|unique:directions,code,{$this->direction->id}",
'education_level_id' => 'required|int|numeric|max:1000', 'education_level_id' => 'required|int|numeric|max:1000',
'education_form_id' => 'required|int|numeric|max:1000', 'education_form_id' => 'required|int|numeric|max:1000',
'code' => [ 'department_id' => 'required|numeric|int|max:1000',
'required', 'entrance-examination.*.examination_type_id' => 'required|numeric|int|max:1000',
'string', 'entrance-examination.*.subject_id' => 'required|numeric|int|max:1000',
'max:255', 'entrance-examination.*.subject_type_id' => 'required|numeric|int|max:1000',
"unique:directions,code,{$this->direction->id}", 'entrance-examination.*.scores' => 'required|numeric|int|max:1000',
], 'entrance-examination.*.position' => 'required|numeric|int|max:1000',
'budget_places' => 'required|int|numeric|max:255',
'paid_places' => 'required|int|numeric|max:255',
'quota' => 'required|int|numeric|max:255',
'cost_paid_place' => 'required|int|numeric|max:255',
'period' => 'required|string|max:255',
'direction_profiles' => 'nullable|array',
'delete.*' => 'nullable|array'
]; ];
} }
} }

View File

@ -2,7 +2,7 @@
@section('content') @section('content')
@auth() @auth()
<h1 class=""> Создать Направление</h1> <h1 class=""> Создать Направление</h1>
{{ Form::open(['url' => route('directions.store'), 'method' => 'POST', 'class' => '']) }} {{ Form::open(['url' => route('directions.update', $direction), 'method' => 'PATCH', 'class' => 'ffff']) }}
<div class="row"> <div class="row">
<div class="col"> <div class="col">
<div class="mt-3"> <div class="mt-3">
@ -110,7 +110,6 @@
</div> </div>
<div class="row"> <div class="row">
<div class="col"> <div class="col">
<div class="mt-3"> <div class="mt-3">
@ -197,91 +196,100 @@
</div> </div>
</div> </div>
<h4 class="mt-3">Добавить вступительные испытания</h4> <div class="d-flex mt-3">
<h4 class="mt-3 m-2 d-inline-block">Добавить вступительные испытания</h4>
<button type="button" class="btn btn-success m-2 d-inline-block btn-sm" name="add" id="add">Добавить
</button>
</div>
<div class="text-center align-items-center entrance-examination" id="entrance-examination" <div class="text-center align-items-center entrance-examination" id="entrance-examination"
name="entrance-examination"> name="entrance-examination">
<div class="row"> @foreach($direction->entranceExaminations as $entranceExamination)
<div class="col"> <div class="row field">
<div class="mt-3"> <div class="col">
{{ Form::label('entrance-examination[0][examination_type_id]', 'Тип экзамена') }} <div class="mt-3">
{{ Form::label("entrance-examination[{$entranceExamination->id}][examination_type_id]", 'Тип экзамена') }}
</div>
<div class="mt-1">
{{ Form::select("entrance-examination[{$entranceExamination->id}][examination_type_id]", $examination_types, $entranceExamination->examinationType->id, ['class' => 'form-select']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first("entrance-examination[{$entranceExamination->id}][examination_type_id]") }}
@endif
</div>
</div> </div>
<div class="mt-1"> <div class="col">
{{ Form::select('entrance-examination[0][examination_type_id]', $examination_types, null, ['class' => 'form-select']) }} <div class="mt-3">
{{ Form::label("entrance-examination[{$entranceExamination->id}][subject_id]", 'Предмет') }}
</div>
<div class="mt-1 col">
{{ Form::select("entrance-examination[{$entranceExamination->id}][subject_id]", $subjects, $entranceExamination->subject->id, ['class' => 'form-select']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first("entrance-examination[{$entranceExamination->id}][subject_id]") }}
@endif
</div>
</div> </div>
<div> <div class="col">
@if ($errors->any()) <div class="mt-3">
{{ $errors->first('entrance-examination[0][examination_type_id]') }} {{ Form::label("entrance-examination[{$entranceExamination->id}][subject_type_id]", 'Тип предмета') }}
@endif </div>
<div class="mt-1 col">
{{ Form::select("entrance-examination[{$entranceExamination->id}][subject_type_id]", $subjectTypes, $entranceExamination->subjectType->id, ['class' => 'form-select']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first("entrance-examination[{$entranceExamination->id}][subject_type_id]") }}
@endif
</div>
</div>
<div class="col">
<div class="mt-3">
{{ Form::label("entrance-examination[{$entranceExamination->id}][scores]", 'Кол-во баллов') }}
</div>
<div class="mt-1 col">
{{ Form::text("entrance-examination[{$entranceExamination->id}][scores]", $entranceExamination->scores, ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first("entrance-examination[{$entranceExamination->id}][scores]") }}
@endif
</div>
</div>
<div class="col">
<div class="mt-3">
{{ Form::label("entrance-examination[{$entranceExamination->id}][position]", 'Позиция') }}
</div>
<div class="mt-1 col">
{{ Form::text("entrance-examination[{$entranceExamination->id}][position]", $entranceExamination->position, ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first("entrance-examination[{$entranceExamination->id}][position]") }}
@endif
</div>
</div>
<div class="col align-items-end">
<div class="mt-3">
{{ Form::label('btn', 'Действия') }}
</div>
<button type="button" class="btn btn-danger col remove-examination-row"
data-id="{{$entranceExamination->id}}">Удалить
</button>
</div> </div>
</div> </div>
<div class="col">
<div class="mt-3">
{{ Form::label('entrance-examination[0][subject_id]', 'Предмет') }}
</div>
<div class="mt-1 col">
{{ Form::select('entrance-examination[0][subject_id]', $subjects, null, ['class' => 'form-select']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('entrance-examination[0][subject_id]') }}
@endif
</div>
</div>
<div class="col">
<div class="mt-3">
{{ Form::label('entrance-examination[0][subject_type_id]', 'Тип предмета') }}
</div>
<div class="mt-1 col">
{{ Form::select('entrance-examination[0][subject_type_id]', $subjectTypes, null, ['class' => 'form-select']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('entrance-examination[0][subject_type_id]') }}
@endif
</div>
</div>
<div class="col">
<div class="mt-3">
{{ Form::label('entrance-examination[0][scores]', 'Кол-во баллов') }}
</div>
<div class="mt-1 col">
{{ Form::text('entrance-examination[0][scores]', '', ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('entrance-examination[0][scores]') }}
@endif
</div>
</div>
<div class="col">
<div class="mt-3">
{{ Form::label('entrance-examination[0][position]', 'Позиция') }}
</div>
<div class="mt-1 col">
{{ Form::text('entrance-examination[0][position]', '', ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('entrance-examination[0][position]') }}
@endif
</div>
</div>
<div class="col align-items-end">
<div class="mt-3">
{{ Form::label('btn', 'Действия') }}
</div>
<button type="button" class="btn btn-success col" name="add" id="add">Добавить</button>
</div>
</div>
</div>
@endforeach
</div>
<div class="mt-3 mb-3"> <div class="mt-3 mb-3">
{{ Form::submit('Изменить Направление', ['class' => 'btn btn-primary']) }} {{ Form::submit('Изменить Направление', ['class' => 'btn btn-primary']) }}
</div> </div>
{{ Form::close() }} {{ Form::close() }}
</div> </div>
@include('admin.catalog.direction.script') @include('admin.catalog.direction.edit_script')
@endauth @endauth
@endsection @endsection

View File

@ -0,0 +1,89 @@
<script type="module">
let i = 0;
$('#add').click(function () {
--i;
$('#entrance-examination').append(
`<div class="row field">
<div class="col">
<div class="mt-3">
<label for="entrance-examination[${i}][examination_type_id]">Тип экзамена</label>
</div>
<div class="mt-1">
<select class="form-select" id="entrance-examination[${i}][examination_type_id]"
name="entrance-examination[${i}][examination_type_id]">
@foreach($examination_types as $value => $type)
<option value="{{ $value }}">{{ $type }}</option>
@endforeach
</select>
</div>
<div>
</div>
</div>
<div class="col">
<div class="mt-3">
<label for="entrance-examination[${i}][subject_id]">Предмет</label>
</div>
<div class="mt-1 col">
<select class="form-select" id="entrance-examination[${i}][subject_id]"
name="entrance-examination[${i}][subject_id]">
@foreach($subjects as $value => $subject)
<option value="{{ $value }}">{{ $subject }}</option>
@endforeach
</select>
</div>
<div>
</div>
</div>
<div class="col">
<div class="mt-3">
<label for="entrance-examination[${i}][subject_type_id]">Тип предмета</label>
</div>
<div class="mt-1 col">
<select class="form-select" id="entrance-examination[${i}][subject_type_id]"
name="entrance-examination[${i}][subject_type_id]">
@foreach($subjectTypes as $value => $subjectType)
<option value="{{ $value }}">{{ $subjectType }}</option>
@endforeach
</select>
</div>
<div>
</div>
</div>
<div class="col">
<div class="mt-3">
<label for="entrance-examination[${i}][scores]">Кол-во баллов</label>
</div>
<div class="mt-1 col">
<input class="form-control" name="entrance-examination[${i}][scores]" type="text" value=""
id="entrance-examination[${i}][scores]">
</div>
<div>
</div>
</div>
<div class="col">
<div class="mt-3">
<label for="entrance-examination[${i}][position]">Позиция</label>
</div>
<div class="mt-1 col">
<input class="form-control" name="entrance-examination[${i}][position]" type="text" value=""
id="entrance-examination[${i}][position]">
</div>
<div>
</div>
</div>
<div class="col align-items-end">
<div class="mt-3">
<label for="btn">Действия</label>
</div>
<button type="button" class="btn btn-danger col remove-examination-row" data-id="${i}">Удалить</button>
</div>
</div>`
);
});
$(document).on('click', '.remove-examination-row', function () {
let id=$(this).data('id');
console.log(id);
$('.ffff').append('<input type="hidden" name="delete['+id+']">')
$(this).parents('.field').remove();
});
</script>