update patch 1.2 crud santri
This commit is contained in:
parent
98a7b8b979
commit
62879aa0f6
|
@ -104,7 +104,14 @@ public function update(Request $request, $id)
|
|||
|
||||
try {
|
||||
$santri = Santri::findOrFail($id);
|
||||
$fotoPath = $santri->foto;
|
||||
$updateData = [
|
||||
'nama' => $request->nama,
|
||||
'alamat' => $request->alamat,
|
||||
'status_santri' => $request->status_santri,
|
||||
'role_santri' => $request->role_santri,
|
||||
'jk' => $request->jk,
|
||||
'tanggal_lahir' => $request->tanggal_lahir,
|
||||
];
|
||||
|
||||
if ($request->hasFile('foto')) {
|
||||
if ($santri->foto && File::exists(public_path($santri->foto))) {
|
||||
|
@ -114,22 +121,10 @@ public function update(Request $request, $id)
|
|||
$foto = $request->file('foto');
|
||||
$fotoName = time() . '_' . $foto->getClientOriginalName();
|
||||
$foto->move(public_path('fotoSantri'), $fotoName);
|
||||
$fotoPath = 'fotoSantri/' . $fotoName;
|
||||
$updateData['foto'] = 'fotoSantri/' . $fotoName;
|
||||
}
|
||||
|
||||
$updateStatus = $santri->update([
|
||||
'nama' => $request->nama,
|
||||
'alamat' => $request->alamat,
|
||||
'status_santri' => $request->status_santri,
|
||||
'role_santri' => $request->role_santri,
|
||||
'jk' => $request->jk,
|
||||
'tanggal_lahir' => $request->tanggal_lahir,
|
||||
'foto' => $fotoPath
|
||||
]);
|
||||
|
||||
if (!$updateStatus) {
|
||||
return redirect()->back()->with('error', 'Gagal memperbarui data: Update gagal di database.');
|
||||
}
|
||||
$updateStatus = $santri->update($updateData);
|
||||
|
||||
return redirect()->back()->with('success', 'Data Berhasil Diubah');
|
||||
} catch (\Throwable $th) {
|
||||
|
|
|
@ -20,7 +20,7 @@ public function up(): void
|
|||
$table->enum('role_santri', ['santri', 'pengurus']);
|
||||
$table->enum('jk', ['laki laki', 'perempuan']);
|
||||
$table->date('tanggal_lahir');
|
||||
$table->string('foto');
|
||||
$table->string('foto')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 184 KiB |
|
@ -0,0 +1,39 @@
|
|||
import { Inertia } from "@inertiajs/inertia";
|
||||
import React, { useState } from "react";
|
||||
|
||||
const DeleteButton = ({ isOpen, onClose, item, tableName }) => {
|
||||
if (!isOpen || !item) return null
|
||||
|
||||
const handleDelete = () => {
|
||||
Inertia.post(`/delete${tableName}/${item.id}`, {
|
||||
onSuccess: () => {
|
||||
onClose();
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="modal modal-open">
|
||||
<div className="modal-box">
|
||||
<h2 className="font-bold text-lg text-center mb-5">
|
||||
Konfirmasi Hapus Data
|
||||
</h2>
|
||||
<p className="text-center mb-4">
|
||||
Apakah Anda yakin ingin menghapus <strong>{item.nama}</strong>?
|
||||
</p>
|
||||
<div className="flex justify-center gap-4">
|
||||
<button onClick={handleDelete} className="btn btn-error text-white">
|
||||
Hapus
|
||||
</button>
|
||||
<button onClick={onClose} className="btn btn-secondary text-white">
|
||||
Batal
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<label className="modal-backdrop" onClick={onClose}></label>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default DeleteButton
|
||||
|
|
@ -23,6 +23,9 @@ const ModalInput = ({ fields, tableName, options, initialData, onClose }) => {
|
|||
|
||||
const formDataObj = new FormData()
|
||||
Object.keys(formData).forEach((key) => {
|
||||
if (key === 'foto' && !(formData[key] instanceof File)) {
|
||||
return
|
||||
}
|
||||
formDataObj.append(key, formData[key])
|
||||
})
|
||||
|
||||
|
@ -31,7 +34,7 @@ const ModalInput = ({ fields, tableName, options, initialData, onClose }) => {
|
|||
forceFormData: true,
|
||||
onError: (errors) => setErrors(errors),
|
||||
onSuccess: () => {
|
||||
document.getElementById('my_modal_7').checked = false
|
||||
document.getElementById('modal_input').checked = false
|
||||
setFormData({})
|
||||
setErrors({})
|
||||
onClose({})
|
||||
|
@ -42,7 +45,7 @@ const ModalInput = ({ fields, tableName, options, initialData, onClose }) => {
|
|||
forceFormData: true,
|
||||
onError: (errors) => setErrors(errors),
|
||||
onSuccess: () => {
|
||||
document.getElementById('my_modal_7').checked = false
|
||||
document.getElementById('modal_input').checked = false
|
||||
setFormData({})
|
||||
setErrors({})
|
||||
}
|
||||
|
@ -111,3 +114,5 @@ const ModalInput = ({ fields, tableName, options, initialData, onClose }) => {
|
|||
};
|
||||
|
||||
export default ModalInput;
|
||||
|
||||
|
||||
|
|
|
@ -1,9 +1,17 @@
|
|||
import React, { useState } from 'react';
|
||||
import { Head } from '@inertiajs/react';
|
||||
import ModalInput from '@/Components/ModalInput';
|
||||
import React, { useState } from 'react'
|
||||
import { Head } from '@inertiajs/react'
|
||||
import ModalInput from '@/Components/ModalInput'
|
||||
import DeleteButton from '@/Components/deleteButton'
|
||||
|
||||
export default function IndexSantri({ santri, fields, options }) {
|
||||
const [selectedSantri, setSelectedSantri] = useState(null);
|
||||
const [selectedSantri, setSelectedSantri] = useState(null)
|
||||
const [isDeleteOpen, setDeleteOpen] = useState(false)
|
||||
|
||||
const openDeleteModal = (item) => {
|
||||
setSelectedSantri(item)
|
||||
setDeleteOpen(true)
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<div className="text-red-900">
|
||||
|
@ -18,7 +26,7 @@ export default function IndexSantri({ santri, fields, options }) {
|
|||
onClose={() => setSelectedSantri(null)}
|
||||
/>
|
||||
|
||||
<label htmlFor="modal_input" className="btn btn-primary">Tambah Santri</label>
|
||||
<label htmlFor="modal_input" className="btn btn-primary" onClick={() => setSelectedSantri(null)}>Tambah Santri</label>
|
||||
|
||||
{santri ? santri.map((item, i) => {
|
||||
return (
|
||||
|
@ -37,10 +45,17 @@ export default function IndexSantri({ santri, fields, options }) {
|
|||
}}>
|
||||
Edit
|
||||
</button>
|
||||
<button className='btn btn-error btn-sm ml-4 text-white' onClick={() => openDeleteModal(item)}>Hapus</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
)
|
||||
}) : ""}
|
||||
<DeleteButton
|
||||
isOpen={isDeleteOpen}
|
||||
onClose={() => setDeleteOpen(false)}
|
||||
item={selectedSantri}
|
||||
tableName="santris"
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
|
@ -29,6 +29,7 @@
|
|||
Route::get('/data-santri', [SantriController::class, 'index'])->name('indexSantri');
|
||||
Route::post('/addsantris', [SantriController::class, 'store'])->name('storeSantri');
|
||||
Route::post('/updatesantris/{id}', [SantriController::class, 'update'])->name('updateSantri');
|
||||
Route::post('/deletesantris/{id}', [SantriController::class, 'destroy'])->name('deleteSantri');
|
||||
|
||||
Route::get('/dashboard', function () {
|
||||
return Inertia::render('Dashboard');
|
||||
|
|
Loading…
Reference in New Issue