add logic in additional controller and additional view for crud
This commit is contained in:
parent
f9eb2397af
commit
8e7b672af8
|
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Additional;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class AdditionalController extends Controller
|
||||
{
|
||||
// Store: Simpan Data Baru
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'nama' => 'required|string|min:3|max:100',
|
||||
'harga' => 'required|numeric|min:0',
|
||||
], [
|
||||
'required' => 'Kolom :attribute wajib diisi.',
|
||||
'numeric' => ':attribute harus berupa angka.',
|
||||
'string' => 'Input :attribute harus berupa teks valid.',
|
||||
'max' => ':attribute melebihi batas, maksimal :max karakter/KB.',
|
||||
'min' => ':attribute minimal harus :min karakter/nilai.',
|
||||
'numeric' => ':attribute harus berupa angka.',
|
||||
], [
|
||||
'nama' => 'nama additional',
|
||||
'harga' => 'harga additional',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return redirect()->back()
|
||||
->withErrors($validator)
|
||||
->withInput()
|
||||
->with('error_modal', 'createAdd'); // Membuka modal tambah additional
|
||||
}
|
||||
|
||||
Additional::create($request->only(['nama', 'harga']));
|
||||
|
||||
return redirect()->back()->with('success', 'Additional berhasil ditambahkan!');
|
||||
}
|
||||
|
||||
// Update: Simpan Perubahan
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
$additional = Additional::findOrFail($id);
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'nama' => 'required|string|min:3|max:100',
|
||||
'harga' => 'required|numeric|min:0',
|
||||
], [
|
||||
'required' => 'Kolom :attribute wajib diisi.',
|
||||
'numeric' => ':attribute harus berupa angka.',
|
||||
'string' => 'Input :attribute harus berupa teks valid.',
|
||||
'max' => ':attribute melebihi batas, maksimal :max karakter/KB.',
|
||||
'min' => ':attribute minimal harus :min karakter/nilai.',
|
||||
'numeric' => ':attribute harus berupa angka.',
|
||||
], [
|
||||
'nama' => 'nama additional',
|
||||
'harga' => 'harga additional',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return redirect()->back()
|
||||
->withErrors($validator)
|
||||
->withInput()
|
||||
->with('error_id_add', $id); // Membuka modal edit additional
|
||||
}
|
||||
|
||||
$additional->update($request->only(['nama', 'harga']));
|
||||
|
||||
return redirect()->back()->with('success', 'Additional berhasil diperbarui!');
|
||||
}
|
||||
|
||||
// Destroy: Hapus Data
|
||||
public function destroy(string $id)
|
||||
{
|
||||
$additional = Additional::findOrFail($id);
|
||||
$additional->delete();
|
||||
|
||||
return redirect()->back()->with('success', 'Additional berhasil dihapus!');
|
||||
}
|
||||
}
|
||||
|
|
@ -117,28 +117,27 @@
|
|||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@forelse ($additional as $ad)
|
||||
@forelse ($additional as $add)
|
||||
<tr>
|
||||
<td style="width: 5%">{{ $loop->iteration }}</td>
|
||||
<td style="width: 65%">{{ $ad->nama }}</td>
|
||||
<td style="width: 15%">Rp {{ number_format($ad->harga, 0, ',', '.') }}</td>
|
||||
<td style="width: 65%">{{ $add->nama }}</td>
|
||||
<td style="width: 15%">Rp {{ number_format($add->harga, 0, ',', '.') }}</td>
|
||||
<td class="col-auto text-center" style="width: 15%">
|
||||
<a href="#" class="btn icon btn-warning btn-action"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#editFoto{{ $ad->id_additional }}">
|
||||
data-bs-target="#editAdd{{ $add->id_additional }}">
|
||||
<i class="bi bi-pencil"></i>
|
||||
</a>
|
||||
|
||||
<a href="#" class="btn icon btn-danger btn-action"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#deleteFoto{{ $ad->id_additional }}">
|
||||
data-bs-target="#deleteAdd{{ $add->id_additional }}">
|
||||
<i class="bi bi-trash"></i>
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
{{-- @include('admin.produk-buket.partials.modal-show')
|
||||
@include('admin.produk-buket.partials.modal-edit')
|
||||
@include('admin.produk-buket.partials.modal-delete') --}}
|
||||
@include('admin.paket-foto.partials.modal-edit-additional')
|
||||
@include('admin.paket-foto.partials.modal-delete-additional')
|
||||
@empty
|
||||
<tr>
|
||||
<td colspan="6" class="text-center p-4">
|
||||
|
|
@ -158,12 +157,8 @@
|
|||
|
||||
</section>
|
||||
@include('admin.paket-foto.partials.modal-create-foto')
|
||||
@include('admin.paket-foto.partials.modal-show-foto')
|
||||
@include('admin.paket-foto.partials.modal-edit-foto')
|
||||
@include('admin.paket-foto.partials.modal-delete-foto')
|
||||
@include('admin.paket-foto.partials.modal-create-additional')
|
||||
@include('admin.paket-foto.partials.modal-edit-additional')
|
||||
@include('admin.paket-foto.partials.modal-delete-additional')
|
||||
|
||||
@push('scripts')
|
||||
<script>
|
||||
// FUNGSI PREVIEW UNIVERSAL (Bisa untuk Create & Edit)
|
||||
|
|
@ -188,31 +183,32 @@ function clearBackdrop() {
|
|||
const backdrops = document.querySelectorAll('.modal-backdrop');
|
||||
backdrops.forEach(b => b.remove());
|
||||
document.body.classList.remove('modal-open');
|
||||
document.body.style.paddingRight = '';
|
||||
}
|
||||
|
||||
// Logic Modal Error tetap sama seperti milikmu
|
||||
@if (session('error_modal'))
|
||||
clearBackdrop();
|
||||
var modalId = "{{ session('error_modal') }}";
|
||||
var targetElement = document.getElementById(modalId);
|
||||
if (targetElement) {
|
||||
new bootstrap.Modal(targetElement).show();
|
||||
}
|
||||
// Error Create Paket Foto
|
||||
@if (session('error_modal') === 'createFoto')
|
||||
new bootstrap.Modal(document.getElementById('createFoto')).show();
|
||||
@endif
|
||||
|
||||
@if (session('error_id'))
|
||||
// Error Create Additional
|
||||
@if (session('error_modal') === 'createAdd')
|
||||
new bootstrap.Modal(document.getElementById('createAdd')).show();
|
||||
@endif
|
||||
|
||||
// Error Edit Paket Foto
|
||||
@if (session('error_id_foto'))
|
||||
clearBackdrop();
|
||||
var errorId = "{{ session('error_id') }}";
|
||||
var editModalId = document.getElementById("editFoto" + errorId) ? "editFoto" + errorId : "editAdd" +
|
||||
errorId;
|
||||
var targetEdit = document.getElementById(editModalId);
|
||||
if (targetEdit) {
|
||||
new bootstrap.Modal(targetEdit).show();
|
||||
}
|
||||
new bootstrap.Modal(document.getElementById("editFoto{{ session('error_id_foto') }}")).show();
|
||||
@endif
|
||||
|
||||
// Error Edit Additional
|
||||
@if (session('error_id_add'))
|
||||
clearBackdrop();
|
||||
new bootstrap.Modal(document.getElementById("editAdd{{ session('error_id_add') }}")).show();
|
||||
@endif
|
||||
});
|
||||
|
||||
// MODAL SHOW IMG
|
||||
function showImage(src) {
|
||||
var modalImg = document.getElementById('img-preview-target');
|
||||
modalImg.src = src;
|
||||
|
|
|
|||
|
|
@ -7,22 +7,32 @@
|
|||
data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
|
||||
<form action="#" method="POST" enctype="multipart/form-data">
|
||||
<div class="modal-body">
|
||||
<form action="{{ route('admin.additional.store') }}" method="POST" enctype="multipart/form-data">
|
||||
@csrf <div class="modal-body">
|
||||
<div class="row">
|
||||
|
||||
<div class="col-12">
|
||||
|
||||
<div class="mb-2">
|
||||
<label class="form-label">Nama Additional</label>
|
||||
<input type="text" class="form-control"style="font-size: 13px;"
|
||||
placeholder="Masukkan Nama Additional">
|
||||
<input type="text" name="nama"
|
||||
class="form-control @error('nama') is-invalid @enderror" style="font-size: 14px;"
|
||||
placeholder="Masukkan Nama Additional" value="{{ old('nama') }}">
|
||||
@error('nama')
|
||||
<div class="invalid-feedback">{{ $message }}</div>
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
<div class="mb-2">
|
||||
<label class="form-label">Harga Additional</label>
|
||||
<input type="number" class="form-control"style="font-size: 13px;"
|
||||
placeholder="Masukkan Harga Additional">
|
||||
<input type="number" name="harga"
|
||||
class="form-control @error('harga') is-invalid @enderror" style="font-size: 14px;"
|
||||
placeholder="Masukkan Harga Additional" value="{{ old('harga') }}">
|
||||
<p class="mb-0"><small class="text-muted mb-0">Dalam Rupiah</small>
|
||||
</p>
|
||||
@error('harga')
|
||||
<div class="invalid-feedback">{{ $message }}</div>
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -1,15 +1,17 @@
|
|||
<div class="modal fade" id="deleteAdd" tabindex="-1" aria-hidden="true">
|
||||
<div class="modal fade" id="deleteAdd{{ $add->id_additional }}" tabindex="-1" aria-hidden="true">
|
||||
<div class="modal-dialog modal-dialog-centered ">
|
||||
<div class="modal-content">
|
||||
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">Hapus Additional</h5> <button type="button" class="btn-close"
|
||||
<h5 class="modal-title">Hapus {{ $add->nama }}</h5> <button type="button" class="btn-close"
|
||||
data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
|
||||
<form action="#" method="POST" enctype="multipart/form-data">
|
||||
<form action="{{ route('admin.additional.destroy', $add->id_additional) }}" method="POST">
|
||||
@csrf
|
||||
@method('DELETE')
|
||||
<div class="modal-body">
|
||||
<p>Apakah anda yakin ingin menghapus nama aditional?</p>
|
||||
<p>Apakah anda yakin ingin menghapus {{ $add->nama }}?</p>
|
||||
</div>
|
||||
<div class="modal-footer justify-content-end border-top-0 pt-0">
|
||||
<button type="submit" class="btn btn-danger rounded-pill tolak px-3 py-2">
|
||||
|
|
@ -21,33 +23,3 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
// Target elemen berdasarkan ID
|
||||
const fileInput = document.getElementById('fileInput');
|
||||
const imgPreview = document.getElementById('img-preview');
|
||||
const placeholder = document.getElementById('placeholder-text');
|
||||
|
||||
fileInput.addEventListener('change', function(event) {
|
||||
const file = event.target.files[0];
|
||||
|
||||
if (file) {
|
||||
// Jika ada file, baca gambarnya
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onload = function(e) {
|
||||
imgPreview.src = e.target.result; // Masukkan data gambar
|
||||
|
||||
// TUKAR TAMPILAN:
|
||||
imgPreview.classList.remove('d-none'); // Munculkan gambar
|
||||
placeholder.classList.add('d-none'); // Sembunyikan teks
|
||||
}
|
||||
|
||||
reader.readAsDataURL(file);
|
||||
} else {
|
||||
// Jika user membatalkan upload (cancel), reset ke awal
|
||||
imgPreview.src = "#";
|
||||
imgPreview.classList.add('d-none');
|
||||
placeholder.classList.remove('d-none');
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
|
|
@ -1,29 +1,42 @@
|
|||
<div class="modal fade" id="editAdd" tabindex="-1" aria-hidden="true">
|
||||
<div class="modal fade" id="editAdd{{ $add->id_additional }}" tabindex="-1" aria-hidden="true">
|
||||
<div class="modal-dialog modal-dialog-centered">
|
||||
<div class="modal-content">
|
||||
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">Edit Additional</h5> <button type="button" class="btn-close"
|
||||
data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
<h5 class="modal-title">Edit {{ $add->nama }}</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
|
||||
<form action="#" method="POST" enctype="multipart/form-data">
|
||||
<form action="{{ route('admin.additional.update', $add->id_additional) }}" method="POST">
|
||||
@csrf
|
||||
@method('PUT')
|
||||
<div class="modal-body">
|
||||
<div class="row">
|
||||
|
||||
<div class="col-12">
|
||||
|
||||
{{-- NAMA ADDITIONAL --}}
|
||||
<div class="mb-2">
|
||||
<label class="form-label">Nama Additional</label>
|
||||
<input type="text" class="form-control"style="font-size: 13px;"
|
||||
placeholder="Masukkan Nama Additional">
|
||||
<input type="text" name="nama" {{-- WAJIB ADA NAME --}}
|
||||
class="form-control @error('nama') is-invalid @enderror" style="font-size: 14px;"
|
||||
placeholder="Masukkan Nama Additional" value="{{ old('nama', $add->nama) }}">
|
||||
@error('nama')
|
||||
<div class="invalid-feedback">{{ $message }}</div>
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
{{-- HARGA ADDITIONAL --}}
|
||||
<div class="mb-2">
|
||||
<label class="form-label">Harga Additional</label>
|
||||
<input type="number" class="form-control"style="font-size: 13px;"
|
||||
placeholder="Masukkan Harga Additional">
|
||||
<input type="number" name="harga" {{-- WAJIB ADA NAME --}}
|
||||
class="form-control @error('harga') is-invalid @enderror" style="font-size: 14px;"
|
||||
placeholder="Masukkan Harga Additional" value="{{ old('harga', $add->harga) }}">
|
||||
<p class="mb-0"><small class="text-muted mb-0">Dalam Rupiah</small></p>
|
||||
@error('harga')
|
||||
<div class="invalid-feedback">{{ $message }}</div>
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Reference in New Issue