mgsmus ceate.blade.php
dosyamın içerisine modal ekleyerek sorunumu hallettim. Ancak;
@extends('layouts.backend.admin.app')
@section('content')
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="card card-flush">
<!--begin::Card header-->
<div class="card-header align-items-center py-5 gap-2 gap-md-5">
<!--begin::Card title-->
<div class="card-title">
</div>
<!--end::Card title-->
</div>
<!--end::Card header-->
<!--begin::Card body-->
<div class="card-body pt-0">
<form id="kt_modal_add_user_form" method="post" class="form" action="{{ route('admin.equipmenttypes.store') }}">
@csrf
<!--begin::Scroll-->
<div class="d-flex flex-column scroll-y me-n7 pe-7" id="kt_modal_add_user_scroll" data-kt-scroll="true" data-kt-scroll-activate="{default: false, lg: true}" data-kt-scroll-max-height="auto" data-kt-scroll-dependencies="#kt_modal_add_user_header" data-kt-scroll-wrappers="#kt_modal_add_user_scroll" data-kt-scroll-offset="300px">
<!--begin::Input group-->
<div class="fv-row mb-7">
<!--begin::Label-->
<label for="code" class="required fw-bold fs-6 mb-2">Sistem Adı</label>
<!--end::Label-->
<!--begin::Input-->
<select class="form-select" data-control="select2" name="equipment_id" data-placeholder="Sistem Seçiniz">
<option></option>
@foreach($equipments as $equipment)
<option value="{{ $equipment->id }}" @selected(old('equipment_id') == $equipment->id)>{{ $equipment->hasTranslation('tr') ? $equipment->translate('tr')->title : '' }}</option>
@endforeach
</select>
<a href="#" class="btn btn-icon-primary btn-text-primary" data-bs-toggle="modal" data-bs-target="#add_system_modal"><span class="svg-icon svg-icon-1"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-plus-square" viewBox="0 0 16 16">
<path d="M14 1a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1h12zM2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H2z"/>
<path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z"/>
</svg></span>Sistem Ekle
</a>
<!--end::Input-->
@if ($errors->has('equipment_id'))
<span class="text-danger">{{ $errors->first('equipment_id') }}</span>
@endif
</div>
<!--end::Input group-->
<!--begin::Input group-->
<div class="fv-row mb-7">
<!--begin::Label-->
<label for="code" class="required fw-bold fs-6 mb-2">Ekipman Kodu</label>
<!--end::Label-->
<!--begin::Input-->
<input type="text" id="code" name="code" class="form-control form-control-solid mb-3 mb-lg-0" placeholder="Ekipman Tipi Kodu" value="{{ old('code') }}" />
@if ($errors->has('code'))
<span class="text-danger">{{ $errors->first('code') }}</span>
@endif
<!--end::Input-->
</div>
<!--end::Input group-->
<!--begin::Input group-->
<div class="fv-row mb-7">
<!--begin::Label-->
<label for="title:tr" class="required fw-bold fs-6 mb-2">Ekipman Adı (Türkçe)</label>
<!--end::Label-->
<!--begin::Input-->
<input type="text" id="title:tr" name="tr[title]" class="form-control form-control-solid mb-3 mb-lg-0" placeholder="Ekipman Tipi Adı (Türkçe)" value="{{ old('tr.title') }}" />
@if ($errors->has('tr.title'))
<span class="text-danger">{{ $errors->first('tr.title') }}</span>
@endif
<!--end::Input-->
</div>
<!--end::Input group-->
<!--begin::Input group-->
<div class="fv-row mb-7">
<!--begin::Label-->
<label for="translations[en][title]" class="required fw-bold fs-6 mb-2">Ekipman Adı (English)</label>
<!--end::Label-->
<!--begin::Input-->
<input type="text" id="en[title]" name="en[title]" class="form-control form-control-solid mb-3 mb-lg-0" placeholder="Ekipman Tipi Adı (English)" value="{{ old('en.title') }}" />
@if ($errors->has('en.title'))
<span class="text-danger">{{ $errors->first('en.title') }}</span>
@endif
<!--end::Input-->
</div>
<!--end::Input group-->
</div>
<!--end::Scroll-->
<!--begin::Actions-->
<div class="text-center pt-15">
<button type="reset" class="btn btn-light me-3" data-kt-users-modal-action="cancel">Vazgeç</button>
<button type="submit" class="btn btn-primary" data-kt-users-modal-action="submit">
<span class="indicator-label">Gönder</span>
<span class="indicator-progress">
Please wait...
<span class="spinner-border spinner-border-sm align-middle ms-2"></span>
</span>
</button>
</div>
<!--end::Actions-->
</form>
<!--end::Form-->
</div>
<!--end::Card body-->
</div>
<!--begin::Modal SİSTEM EKLEME- New Target-->
<div class="modal fade" id="add_system_modal" tabindex="-1" aria-hidden="true">
<!--begin::Modal dialog-->
<div class="modal-dialog modal-dialog-centered mw-650px">
<!--begin::Modal content-->
<div class="modal-content rounded">
<!--begin::Modal header-->
<div class="modal-header pb-0 border-0 justify-content-end">
<!--begin::Close-->
<div class="btn btn-sm btn-icon btn-active-color-primary" data-bs-dismiss="modal">
<!--begin::Svg Icon | path: icons/duotune/arrows/arr061.svg-->
<span class="svg-icon svg-icon-1">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none">
<rect opacity="0.5" x="6" y="17.3137" width="16" height="2" rx="1" transform="rotate(-45 6 17.3137)" fill="black" />
<rect x="7.41422" y="6" width="16" height="2" rx="1" transform="rotate(45 7.41422 6)" fill="black" />
</svg>
</span>
<!--end::Svg Icon-->
</div>
<!--end::Close-->
</div>
<!--begin::Modal header-->
<!--begin::Modal body-->
<div class="modal-body scroll-y px-10 px-lg-15 pt-0 pb-15">
<!--begin:Form-->
{{-- <form name="ajax-contact-form" id="addSystemForm" method="post" action=" {{ route('admin.equipments.store') }}">--}}
<form id="addSystemForm">
@csrf
<!--begin::Input group-->
<div class="fv-row mb-7">
<!--begin::Label-->
<label for="systemCode" class="required fw-bold fs-6 mb-2">Sistem Kodu</label>
<!--end::Label-->
<!--begin::Input-->
<input type="text" id="systemCode" name="systemCode" class="form-control form-control-solid mb-3 mb-lg-0" placeholder="Sistem Kodu" value="{{ old('code') }}" />
<span class="text-danger" id="systemCode-error"></span>
<!--end::Input-->
</div>
<!--end::Input group-->
<!--begin::Input group-->
<div class="fv-row mb-7">
<!--begin::Label-->
<label for="titletr" class="required fw-bold fs-6 mb-2">Sistem Adı (Türkçe)</label>
<!--end::Label-->
<!--begin::Input-->
<input type="text" id="titletr" name="titletr" class="form-control form-control-solid mb-3 mb-lg-0" placeholder="Sistem Adı (Türkçe)" value="{{ old('titleTR') }}" />
<span class="text-danger" id="titletr-error"></span>
<!--end::Input-->
</div>
<!--end::Input group-->
<!--begin::Actions-->
<div class="text-center">
<button type="reset" id="kt_modal_new_target_cancel" class="btn btn-light me-3">Cancel</button>
<button class="btn btn-success btn-submit">
<span class="indicator-label">Submit</span>
<span class="indicator-progress">Please wait...
<span class="spinner-border spinner-border-sm align-middle ms-2"></span></span>
</button>
</div>
<!--end::Actions-->
</form>
<!--end:Form-->
</div>
<!--end::Modal body-->
</div>
<!--end::Modal content-->
</div>
<!--end::Modal dialog-->
</div>
<!--end::Modal SİSTEM EKLEME- New Target-->
@endsection
@section('javascript')
<script type="text/javascript">
$('#addSystemForm').on('submit',function(e){
e.preventDefault();
let systemCode = $('#systemCode').val();
let titletr = $('#titletr').val();
$.ajax({
url: "{{ route('admin.equipments.store') }}",
type:"POST",
data:{
"_token": "{{ csrf_token() }}",
systemCode:systemCode,
titletr:titletr,
},
success:function(response){
console.log(response);
if (response) {
$('#success-message').text(response.success);
$("#addSystemForm")[0].reset();
$('#add_system_modal').modal('hide');
}
},
error: function(response) {
$('#systemCode-error').text(response.responseJSON.errors.systemCode);
$('#titletr-error').text(response.responseJSON.errors.titletr);
}
});
});
</script>
@endsection
EquipmentController.php
<?php
namespace App\Http\Controllers\Backend\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreEquipmentRequest;
use App\Http\Requests\Admin\UpdateEquipmentRequest;
use App\Models\Equipment;
use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use App;
class EquipmentController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$equipments = Equipment::all();
return view('backend.admin.equipment.index', compact('equipments'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
// return LaravelLocalization::getSupportedLocales();
return view('backend.admin.equipment.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'systemCode' => 'required',
'titletr' => 'required',
]);
$data = [
'code' => $request->input('systemCode'),
'tr' => [
'title' => $request->input('titletr'),
],
'en' => [
'title' => $request->input('title:en') ? $request->input('title:en') : $request->input('titletr') ,
],
];
$system = Equipment::create($data);
return response()->json(['success'=>'Form is successfully submitted!', 'system' => $system]);
// return redirect()->route('admin.equipments.index');
}
/**
* Display the specified resource.
*
* @param \App\Models\Equipment $equipment
* @return \Illuminate\Http\Response
*/
public function show(Equipment $equipment)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Equipment $equipment
* @return \Illuminate\Http\Response
*/
public function edit(Equipment $equipment)
{
return view('backend.admin.equipment.edit', compact('equipment'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Equipment $equipment
* @return \Illuminate\Http\Response
*/
public function update(UpdateEquipmentRequest $request, Equipment $equipment)
{
$data = [
'code' => $request->input('code'),
'tr' => [
'title' => $request->input('title:tr'),
],
'en' => [
'title' => $request->input('title:en'),
],
];
$equipment->update($data);
return redirect()->route('admin.equipments.edit', $equipment->id);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Equipment $equipment
* @return \Illuminate\Http\Response
*/
public function destroy(Equipment $equipment)
{
$equipment->delete();
return redirect()->route('admin.equipments.index')
->with('success','Product deleted successfully');
}
}
Soru 1: Controller dosyamın public function store(StoreEquipmentRequest $request)
olarak değiştirdiğimde konsolda Uncaught TypeError: can't access property "systemCode", response.responseJSON.errors is undefined
hata mesajını veriyor. Doğrulama adımlarını controller içerisinde yaparsam sorun olmuyor. Bu hatamı nasıl düzeltebilirim ?
StoreEquipmentRequest.php
dosya içeriği;
<?php
namespace App\Http\Requests\Admin;
use Illuminate\Foundation\Http\FormRequest;
class StoreEquipmentRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'systemCode' => 'required|unique:equipment|max:150',
'titletr' => 'required|max:255',
];
}
public function attributes()
{
return [
'titletr' => 'Türkçe Başlık',
'systemCode' => 'Ekipman Kodu',
];
}
}
Soru 2 : modal ile eklediğim kaydı sayfa yenilemeden selectbox içerisinde nasıl gösteririm ?
Yardımlarınız için şimdiden teşekkür ederim.