Postingan lainnya
Error laravel update data request NULL
Terdapat masalah ketika update, error karena request nya null, tapi ketika insert request nya ada, padahal ketika update value nya sudah tampil menggunakan ajax method edit (tertera code nya), tapi pada saat klik submit request nya null
mohon bantuannya, terimakasih
Form
<!-- Modal form -->
<div class="modal fade" id="modal-form" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form id="form" enctype="multipart/form-data">
@csrf
<div class="modal-body">
<div class="row">
<div class="col-lg-12">
<input type="text" name="id" id="id">
<div class="form-group">
<label for="kode">Kode</label>
<input type="text" class="form-control" id="kode" name="kode" placeholder="Contoh: BRO" autofocus autocomplete="">
</div>
</div>
<div class="col-lg-12">
<div class="form-group">
<label for="name">Nama</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Contoh: Brownies" autofocus autocomplete="">
</div>
</div>
<div class="col-lg-10" id="input-gambar">
<div class="form-group">
<label for="gambar">Gambar</label>
<div class="custom-file">
<input type="file" class="custom-file-input" name="gambar" id="gambar" lang="en">
<label class="custom-file-label" for="gambar"></label>
</div>
<small class="text-danger">* Hanya format .png .jpg .jpeg max: 1Mb yang dibolehin</small>
</div>
</div>
<div class="col-lg-2" id="store_image">
<div class="image-previous"></div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal" id="btn-close">Tutup</button>
<button type="submit" class="btn btn-primary" id="btn-save">Simpan Data</button>
</div>
</form>
</div>
</div>
</div>
javascript ajax proses insert dan update
// Insert and update function
$("#form").on("submit", function (event) {
event.preventDefault();
var form = $("#form"),
id = $("#id").val();
if ($("#btn-save").val() == "create-data") {
url = baseUrl + "kategori/create";
method = "POST";
} else {
url = baseUrl + "kategori/" + id;
method = "PUT";
}
$.ajax({
url: url,
method: method,
data: new FormData($("#modal-form form")[0]),
dataType: "JSON",
contentType: false,
processData: false,
success: function (response) {
$("#modal-form").modal("hide");
$("#dataTable").DataTable().ajax.reload();
Swal.fire("Berhasil!", "Data berhasil disimpan", "success");
},
error: function (xhr) {
var response = xhr.responseJSON;
if ($.isEmptyObject(response) == false) {
$.each(response.errors, function (key, value) {
$("#" + key)
.closest(".form-group")
.addClass("has-error")
.append(
'<small class="help-block text-danger">' + value + "</small>"
);
});
}
},
});
});
javascript ajax method edit
// Function view data edit
function editForm(id) {
$.ajax({
url: baseUrl + 'kategori/' + id + '/edit',
type: 'GET',
dataType: 'JSON',
success: function (data) {
$('#id').val(data.id)
$('#name').val(data.name)
$('#kode').val(data.code)
let html = ''
html += '<img src="' + data.image_path + data.image_random_name + '" class="img-table" alt="' + data.name + '">'
$('.image-previous').html(html)
$('#modal-form').modal('show')
$('#modal-title').html("Edit Kategori")
$('#btn-save').val("update-data")
},
error: function () {
alert("Nothing Data");
}
})
}
controller method edit
public function edit($id)
{
$data = Kategori_m::findOrFail($id);
return response()->json($data);
}
controller method update
public function update(Request $request, $id)
{
dd($request->all());
// $data = Kategori_m::find($id);
// $data->name = $request['name'];
// $data->slug = Str::slug($request['name']);
// $data->code = $request['kode'];
// $data->update();
// return response()->json($data);
}
route insert dan update
Route::post('/kategori/create', 'KategoriController@store');
Route::get('/kategori/{id}/edit', 'KategoriController@edit');
Route::put('/kategori/{id}', 'KategoriController@update');
0
Belum ada Jawaban. Jadi yang pertama Jawaban
Login untuk ikut Jawaban