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
<?php
use App\Http\Controllers\Admin\KategoriController;
use App\Http\Controllers\TransaksiController;
use Illuminate\Support\Facades\Route;
......@@ -19,3 +20,8 @@
});
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