Commit da30bf9d by Triyah Fatmawati

CRUD Master Kategori

parent 7fe66f0d
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Support\ValidationRule;
use App\Models\Admin\MsKategori;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redirect;
class KategoriController extends Controller
{
public function index() {
$kategori = MsKategori::query()->get();
$data = [
'kategori' => $kategori
];
return view('Admin.Kategori.index', $data);
}
public function store(Request $request){
$rule = [
'kodekategori' => 'required|string',
'namakategori' => 'required|string',
'poinsaku' => 'required|string'
];
$request->validate($rule, ValidationRule::getErrorMessage($rule));
DB::beginTransaction();
try{
$kategori = [
'kode_kategori' => strip_tags($request->kodekategori),
'nama_kategori' => strip_tags($request->namakategori),
'poin_saku' => strip_tags($request->poinsaku),
'status' => $request->status
];
MsKategori::query()->create($kategori);
DB::commit();
return Redirect::route('kategori.index')
->with('success', 'Data kategori berhasil ditambahkan');
}
catch(Exception $e){
Log::error($e);
DB::rollBack();
return Redirect::route('kategori.index')
->with('error', 'Data kategori gagal ditambahkan');
}
}
public function update(Request $request, $id){
$rule = [
'kodekategori' => 'required|string',
'namakategori' => 'required|string',
'poinsaku' => 'required|string'
];
$request->validate($rule, ValidationRule::getErrorMessage($rule));
DB::beginTransaction();
try{
$kategori = [
'kode_kategori' => strip_tags($request->kodekategori),
'nama_kategori' => strip_tags($request->namakategori),
'poin_saku' => strip_tags($request->poinsaku),
'status' => $request->status
];
MsKategori::where('id', $id)->update($kategori);
DB::commit();
return Redirect::route('kategori.index')
->with('success', 'Data kategori berhasil diupdate');
}
catch(Exception $e){
Log::error($e);
DB::rollBack();
return Redirect::route('kategori.index')
->with('error', 'Data kategori gagal diupdate');
}
}
public function destroy($id) {
DB::beginTransaction();
try{
MsKategori::where('id', $id)->delete();
DB::commit();
return Redirect::route('kategori.index')
->with('success', 'Data kategori berhasil diupdate');
}
catch(Exception $e){
Log::error($e);
DB::rollBack();
return Redirect::route('kategori.index')
->with('error', 'Data kategori gagal diupdate');
}
}
}
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class UserController extends Controller
{
//
}
<?php
namespace App\Http\Support;
class ValidationRule{
const WARNING = [
'string' => 'must be a string',
'email' => 'tidak valid',
'mimes' => 'tipe file tidak sesuai',
'unique' => 'must be unique',
'required' => 'tidak boleh kosong',
'unique' => 'sudah digunakan',
'numeric' => 'harus berupa nomor',
'max' => 'tidak boleh lebih dari',
'min' => 'tidak boleh kurang dari',
'before_or_equal' => 'maksimal hari ini',
'after_or_equal' => 'minimal hari ini',
];
public static function getErrorMessage($validationRules) {
$messages = [];
foreach ($validationRules as $key => $rules) {
$rulesArr = explode('|', $rules);
foreach ($rulesArr as $rule) {
$ruleArr = explode('|', $rule);
$ruleParams = explode(':', $ruleArr[0]);
$ruleKey = $ruleParams[0];
$message = str_replace('inputs.', '', $key);
$message = str_replace(['.', '_'], ' ', $message).' '.self::WARNING[$ruleKey].($ruleKey == 'max' || $ruleKey == 'min' ? ' '.$ruleParams[1].' characters' : '');
$messages[$key.'.'.$ruleKey] = $message;
}
}
return $messages;
}
}
?>
<?php
namespace App\Models\Admin;
use App\Traits\Uuid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class MsKategori extends Model
{
use HasFactory;
use Uuid;
public $incrementing = false;
protected $table = 'ms_kategori';
protected $keyType = 'string';
protected $fillable = [
'id',
'kode_kategori',
'nama_kategori',
'poin_saku',
'status'
];
}
<?php
namespace App\Models\Admin;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasFactory;
}
<?php
namespace App\Traits;
use Illuminate\Support\Str;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
trait Uuid
{
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
try {
$model->id = (string) Str::uuid(); // generate uuid
// Change id with your primary key
} catch (UnsatisfiedDependencyException $e) {
abort(500, $e->getMessage());
}
});
}
}
\ No newline at end of file
@extends('layouts.master')
@section('content')
<div class="container-xxl flex-grow-1 container-p-y">
<div class="card">
<div class="row layout-top-spacing" id="cancel-row">
<h5 class="card-header" style="padding-left: 35px">Daftar Kategori</h5>
<div class="card-body">
<div class="row">
<div class="col-md-3" style="padding-left: 30px">
<button type="button" class="btn btn-primary text-right" data-bs-toggle="modal" data-bs-target="#addNewKategoriModal" >
Tambah Kategori
</button>
</div>
</div>
</div>
<div class="card-body">
<div class="table-responsive text-nowrap">
<table class="table">
<thead class="table-light" align="center">
<tr>
<th>No.</th>
<th>Kode Kategori</th>
<th>Nama Kategori</th>
<th>Poin SAKU</th>
<th>Status</th>
<th>Aksi</th>
</tr>
</thead>
<tbody class="table-border-bottom-0">
@php
$no = 1;
@endphp
@foreach ($kategori as $kat)
@php
if($kat->status == "1"){
$status = "Aktif";
}
else{
$status = "Nonaktif";
}
@endphp
<tr style="text-align: center">
<td>{{ $no }}</td>
<td style="text-align: center">{{ $kat->kode_kategori }}</td>
<td>{{ $kat->nama_kategori }}</td>
<td>{{ $kat->poin_saku }}</td>
<td>{{ $status }}</td>
<td>
<button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#editKategoriModal{{$kat->id}}">Edit</button>
<button type="button" class="btn btn-danger btn-sm" data-bs-toggle="modal" data-bs-target="#deleteKategoriModal{{$kat->id}}">Hapus</button>
</td>
</tr>
<!-- Modal Update Kategori -->
<div class="modal fade" id="editKategoriModal{{$kat->id}}" tabindex="-1" aria-labelledby="tambahAntrian" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<form method="POST" action="{{ route('kategori.update', ['kategori' => $kat->id]) }}" enctype="multipart/form-data" id="registrasi">
@method('PUT')
@csrf
<div class="modal-header">
<h1 class="address-title text-center mb-1" id="addNewKategoriTitle">Edit Kategori</h1>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-6">
<label class="form-label" for="kodekategori">Kode Kategori</label>
<input type="text" class="form-control" id="kodekategori" name="kodekategori" placeholder="Kode Kategori" data-msg="Masukkan Kode Kategori" value="{{$kat->kode_kategori}}" required />
@if ($errors->has('kodekategori'))
<label id="login-error" class="error" for="kodekategori" style="color: red">{{$errors->first('kodekategori')}}</label>
@endif
</div>
<div class="col-md-6">
<label class="form-label" for="poinsaku">Poin Saku</label>
<input type="text" id="poinsaku" name="poinsaku" class="form-control" placeholder="Poin SAKU" data-msg="Masukkan Poin SAKU" value="{{$kat->poin_saku}}" required />
@if ($errors->has('poinsaku'))
<label id="login-error" class="error" for="poinsaku" style="color: red">{{$errors->first('poinsaku')}}</label>
@endif
</div>
</div>
<div class="col-md-12">
<label class="form-label" for="namakategori">Nama Kategori</label>
<input type="text" id="namakategori" name="namakategori" class="form-control" placeholder="Nama Kategori" data-msg="Masukkan Nama Kategori" value="{{$kat->nama_kategori}}" required />
@if ($errors->has('namakategori'))
<label id="login-error" class="error" for="namakategori" style="color: red">{{$errors->first('namakategori')}}</label>
@endif
</div>
<hr>
<div class="col-md-12">
<label class="form-label" for="status">Status</label>
<select name="status" id="status" class="form-control">
<option value="0" @if ($kat->status == "0") {{"selected"}} @endif>Tidak Aktif</option>
<option value="1" @if ($kat->status == "1") {{"selected"}} @endif>Aktif</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Submit</button>
<button type="reset" class="btn btn-outline-secondary" data-bs-dismiss="modal" aria-label="Close">Cancel</button>
</div>
</form>
</div>
</div>
</div>
<!-- End Modal Update Kategori -->
<!-- Modal Delete Kategori -->
<div class="modal fade" id="deleteKategoriModal{{$kat->id}}" tabindex="-1" aria-labelledby="tambahAntrian" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<form method="POST" action="{{ route('kategori.destroy', ['kategori' => $kat->id ]) }}" enctype="multipart/form-data">
@csrf
@method("DELETE")
<div class="modal-header">
<h1 class="address-title text-center mb-1" id="addNewKategoriTitle">Hapus Kategori</h1>
</div>
<div class="modal-body">
<div class="form-group" style="text-align: center">
<h5 class="address-title text-center mb-3 mt-1" id="addNewAddressTitle">Apakah anda yakin ingin menghapus kategori ini?</h5>
</div>
<div class="form-group" style="text-align: center">
<button type="submit" class="btn btn-primary ml-1">Delete</button>
<button type="button" class="btn btn-light-secondary" data-dismiss="modal">Cancel</button>
</div>
</div>
</form>
</div>
</div>
</div>
<!-- End Modal Delete Kategori -->
@php
$no++;
@endphp
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Modal Tambah Kategori -->
<div class="modal fade" id="addNewKategoriModal" tabindex="-1" aria-labelledby="tambahAntrian" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<form method="POST" action="{{ route('kategori.store') }}" enctype="multipart/form-data" id="registrasi">
@csrf
<div class="modal-header">
<h1 class="address-title text-center mb-1" id="addNewKategoriTitle">Tambah Kategori</h1>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-6">
<label class="form-label" for="kodekategori">Kode Kategori</label>
<input type="text" class="form-control" id="kodekategori" name="kodekategori" placeholder="Kode Kategori" data-msg="Masukkan Kode Kategori" value="" required />
@if ($errors->has('kodekategori'))
<label id="login-error" class="error" for="kodekategori" style="color: red">{{$errors->first('kodekategori')}}</label>
@endif
</div>
<div class="col-md-6">
<label class="form-label" for="poinsaku">Poin Saku</label>
<input type="text" id="poinsaku" name="poinsaku" class="form-control" placeholder="Poin SAKU" data-msg="Masukkan Poin SAKU" required />
@if ($errors->has('poinsaku'))
<label id="login-error" class="error" for="poinsaku" style="color: red">{{$errors->first('poinsaku')}}</label>
@endif
</div>
</div>
<div class="col-md-12">
<label class="form-label" for="namakategori">Nama Kategori</label>
<input type="text" id="namakategori" name="namakategori" class="form-control" placeholder="Nama Kategori" data-msg="Masukkan Nama Kategori" required />
@if ($errors->has('namakategori'))
<label id="login-error" class="error" for="namakategori" style="color: red">{{$errors->first('namakategori')}}</label>
@endif
</div>
<div class="col-md-12">
<label class="form-label" for="status">Status</label>
<select name="status" id="status" class="form-control">
<option value="0">Tidak Aktif</option>
<option value="1">Aktif</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Submit</button>
<button type="reset" class="btn btn-outline-secondary" data-bs-dismiss="modal" aria-label="Close">Cancel</button>
</div>
</form>
</div>
</div>
</div>
<!-- End Modal Tambah Kategori -->
</div>
@endsection
@push('js')
@endpush
<?php <?php
use App\Http\Controllers\Admin\KategoriController;
use App\Http\Controllers\TransaksiController; use App\Http\Controllers\TransaksiController;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
...@@ -19,3 +20,8 @@ ...@@ -19,3 +20,8 @@
}); });
Route::get('/transaksi', [TransaksiController::class, 'index'])->name('admin.index-transaksi'); Route::get('/transaksi', [TransaksiController::class, 'index'])->name('admin.index-transaksi');
// Admin
Route::resource('kategori', KategoriController::class);
// End of Admin
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment