108 lines
2.9 KiB
PHP
108 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Kopi;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class KopiController extends Controller
|
|
{
|
|
public function index() {
|
|
$data = Kopi::all();
|
|
return view('admin.pages.kopi.kopi', compact('data'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('admin.pages.kopi.createkopi',);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validateData = $request->validate([
|
|
'nama_kopi' => 'required',
|
|
'harga_beli' => 'required',
|
|
'harga_jual' => 'required',
|
|
'gambar' => 'image|file'
|
|
]);
|
|
|
|
// Bersihkan format mata uang sebelum menyimpan ke database
|
|
$validateData['harga_beli'] = preg_replace('/[^0-9]/', '', $validateData['harga_beli']);
|
|
$validateData['harga_jual'] = preg_replace('/[^0-9]/', '', $validateData['harga_jual']);
|
|
|
|
if ($request->file('gambar')) {
|
|
$validateData['gambar'] = $request->file('gambar')->store('kopi');
|
|
}
|
|
|
|
$validateData['stok'] = 0;
|
|
|
|
Kopi::create($validateData);
|
|
|
|
return redirect('/kopi')->with('success', 'data berhasil ditambahkan');
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id)
|
|
{
|
|
$data = Kopi::find($id);
|
|
return view('admin.pages.kopi.detailkopi', compact('data'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id)
|
|
{
|
|
$data = Kopi::find($id);
|
|
return view('admin.pages.kopi.editkopi', compact('data'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, string $id)
|
|
{
|
|
$user = User::find($id);
|
|
$validateData = $request->validate([
|
|
'nama_kopi' => 'required',
|
|
'harga_beli' => 'required',
|
|
'harga_jual' => 'required',
|
|
'gambar' => 'image|file'
|
|
]);
|
|
|
|
// Bersihkan format mata uang sebelum menyimpan ke database
|
|
$validateData['harga_beli'] = preg_replace('/[^0-9]/', '', $validateData['harga_beli']);
|
|
$validateData['harga_jual'] = preg_replace('/[^0-9]/', '', $validateData['harga_jual']);
|
|
|
|
if ($request->file('gambar')) {
|
|
if ($request->oldGambar) {
|
|
Storage::delete($request->oldGambar);
|
|
}
|
|
$validateData['gambar'] = $request->file('gambar')->store('kopi');
|
|
}
|
|
|
|
Kopi::where('id', $id)->update($validateData);
|
|
|
|
return redirect('/kopi')->with('success', 'data berhasil diubah');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
$gambar = Kopi::where('id', $id)->pluck('gambar')->first();
|
|
if ($gambar) {
|
|
Storage::delete($gambar);
|
|
}
|
|
Kopi::destroy($id);
|
|
return redirect('/kopi')->with('delete', 'data berhasil dihapus');
|
|
}
|
|
}
|