<?php
namespace App\Http\Controllers;
use App\Helpers\Helper;
use App\Models\Toko;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class TokoController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$datas = Toko::all();
return view('toko.index', compact('datas'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('toko.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$request->validate([
'nama_toko' => 'required|string|max:255',
]);
DB::beginTransaction();
try {
$toko = Toko::create([
'nama_toko' => $request->nama_toko,
'alamat_toko' => $request->alamat_toko ?? null,
'no_telp_toko' => $request->no_telp_toko ?? null,
]);
Helper::addUserLog('menambah data toko '.$toko->nama_toko);
DB::commit();
return redirect()->route('toko.index')->with('success', 'Toko berhasil ditambahkan');
} catch (\Exception $e) {
DB::rollBack();
return redirect()->back()->withErrors(['error' => 'Gagal menambahkan toko: ' . $e->getMessage()]);
}
}
/**
* Display the specified resource.
*/
public function show(Toko $toko)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Toko $toko)
{
return view('toko.edit', ['data' => $toko]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Toko $toko)
{
$request->validate([
'nama_toko' => 'required|string|max:255',
]);
DB::beginTransaction();
try {
$toko->update([
'nama_toko' => $request->nama_toko,
'alamat_toko' => $request->alamat_toko ?? null,
'no_telp_toko' => $request->no_telp_toko ?? null,
]);
Helper::addUserLog('mengubah data toko '.$toko->nama_toko);
DB::commit();
return redirect()->route('toko.index')->with('success', 'Toko berhasil diperbarui');
} catch (\Exception $e) {
DB::rollBack();
return redirect()->back()->withErrors(['error' => 'Gagal memperbarui toko: ' . $e->getMessage()]);
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Toko $toko)
{
DB::beginTransaction();
try {
$toko->stok_item_tokos()->delete(); // Hapus stok item terkait
$toko->tokoables()->delete(); // Hapus relasi tokoable terkait
$toko->delete();
Helper::addUserLog('menghapus data toko '.$toko->nama_toko);
DB::commit();
return redirect()->route('toko.index')->with('success', 'Toko berhasil dihapus');
} catch (\Exception $e) {
DB::rollBack();
return redirect()->back()->withErrors(['error' => 'Gagal menghapus toko: ' . $e->getMessage()]);
}
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]