order_gizi/app/Http/Controllers/MasterMcuController.php

118 lines
3.0 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\MasterMcu;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class MasterMcuController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$payload = [
'title' => 'Master MCU'
];
return view('dashboard.master.mcu.index', $payload);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
try {
DB::connection('dbOrderGizi')->beginTransaction();
$datas = request('data');
foreach ($datas as $data) {
$payload =[
'nama_mcu' => $data['nama_mcu']
];
if($data['harga']){
$harga = $data['harga'] ? str_replace('.', '', $data['harga']) : null;
$payload['harga'] = $harga;
}
MasterMcu::create($payload);
}
DB::connection('dbOrderGizi')->commit();
return response()->json([
'status' => true,
'message' => 'Data berhasil ditambahkan!'
]);
} catch (\Throwable $th) {
DB::connection('dbOrderGizi')->rollBack();
return response()->json([
'status' => false,
'message' => 'Data gagal ditambahkan!',
'errors' => $th->getMessage()
]);
}
}
/**
* Display the specified resource.
*/
public function show(MasterMcu $masterMcu)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(MasterMcu $masterMcu)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(string $id)
{
$masterMcu = MasterMcu::where('menu_mcu_id', $id)->first();
$payload =[
'nama_mcu' => request('nama_mcu'),
];
if(request('harga')){
$harga = request('harga') ? str_replace('.', '', request('harga')) : null;
$payload['harga'] = $harga;
}
$masterMcu->update($payload);
return response()->json([
'status' => true,
'message' => 'Data berhasil diperbarui!'
]);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
$masterMcu = MasterMcu::where('menu_mcu_id', $id)->first();
$masterMcu->update([
'statusenabled' => false
]);
return response()->json([
'status' => true,
'message' => 'Data berhasil dihapus!'
]);
}
public function datatable(){
return MasterMcu::where('statusenabled', true)->get();
}
}