Anons79 Mini Shell

Directory : /home/aplikasiposinfo/www/sip.aplikasipos.info/app/Http/Controllers/
Upload File :
Current File : /home/aplikasiposinfo/www/sip.aplikasipos.info/app/Http/Controllers/ProdukController.php

<?php

namespace App\Http\Controllers;

use App\Models\Produk;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Database\QueryException;

class ProdukController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['title'] = 'SIP - Produk';
        $data['produk'] = Produk::all();
        return view('produk.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $data['title'] = 'SIP - Tambah Produk';
        return view('produk.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'nama' => 'required',
            'gambar' => 'image|file|mimes:jpg,jpeg,png,gif|max:2048',
        ]);

        $data['nama'] = $request->nama;

        if ($request->hasFile('gambar')) {
            $foto_file = $request->file('gambar');
            $foto_ekstensi = $foto_file->extension();
            $foto_baru = "Komoditi-" . date('ymdhis') . ".$foto_ekstensi";
            $foto_file->move(public_path('GambarKomoditi'), $foto_baru);
            $data['gambar'] = $foto_baru;
        }

        Produk::create($data);
        return redirect()->route('produk.index')->with('success',  'Berhasil menambah data');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $data['title'] = 'SIP - Edit produk';
        $data['item'] = Produk::where('id', $id)->first();
        return view('produk.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        $request->validate([
            'nama' => 'required',
            'gambar' => 'image|file|mimes:jpg,jpeg,png,gif|max:2048',
        ]);

        $data['nama'] = $request->nama;

        if ($request->hasFile('gambar')) {
            $foto_file = $request->file('gambar');
            $foto_ekstensi = $foto_file->extension();
            $foto_baru = "Komoditi-" . date('ymdhis') . ".$foto_ekstensi";
            $foto_file->move(public_path('GambarKomoditi'), $foto_baru);
            $data['gambar'] = $foto_baru;
            $getFoto = Produk::where('id', $id)->first();
            $foto_lama = $getFoto->gambar;
            File::delete(public_path('GambarKomoditi') . "/" . $foto_lama);
        }

        Produk::where('id', $id)->update($data);
        return redirect()->route('produk.index')->with('success',  'Berhasil mengubah data');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        try {
            $getFoto = Produk::where('id', $id)->first();
            $foto_lama = $getFoto->gambar;
            File::delete(public_path('GambarKomoditi') . "/" . $foto_lama);
            Produk::where('id', $id)->delete();
            return redirect()->route('produk.index')->with('success',  'Berhasil menghapus data');
        } catch (QueryException $e) {
            return back()->with('error', 'Tidak dapat menghapus data yang berelasi!');
        }
    }
}

Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]