ajax()) { $data = Produk::select('*'); // Convert the Eloquent Collection to a regular PHP array $data->each(function ($item, $key) { $item->rowIndex = $key + 1; }); return Datatables::of($data) ->addIndexColumn() ->addColumn('harga', function ($row) { return 'Rp. ' . number_format($row->price, 0, ',', '.'); }) ->rawColumns(['harga']) ->make(true); } $data = [ 'subtitle' => 'Semua Produk', 'button' => true, 'module' => [ 'url' => app_url('produk/create'), 'name' => 'Tambah Baru' ] ]; return view('admin.app.content.produk.index', compact('data')); } public function create() { $data = [ 'subtitle' => 'Tambah baru', ]; return view('admin.app.content.produk.add', compact('data')); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $validator = Validator::make($request->all(), [ 'name' => 'required', 'stok' => 'required', 'price' => 'required' ]); if ($validator->fails()) { return redirect()->back()->withInput()->with('error', 'Unexpected error, please try again. code: ' . $validator->errors()->first()); } $input = $request->all(); $post = new Produk([ 'name' => $input['name'], // Membersihkan input judul menggunakan Purifier 'stok' => $input['stok'], // Membersihkan input deskripsi menggunakan Purifier 'price' => $input['price'] ]); $check = Produk::where('name', $input['name'])->count(); if ($check == 0) { if ($post->save()) { return redirect()->route('produk')->with('success', 'You have successfully added data'); } else { return redirect()->route('produk')->with('error', 'An error occurred in the query'); } } else { return redirect()->route('produk')->with('error', 'Title already exists'); } } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $data = [ 'subtitle' => 'Edit Produk' ]; $akun = Produk::FindOrFail($id); return view('admin.app.content.produk.edit', compact('data', 'id', 'akun')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // Validasi input sebelum memperbarui data $validator = Validator::make($request->all(), [ 'name' => 'required', 'stok' => 'required', 'price' => 'required' ]); if ($validator->fails()) { return redirect()->back()->withInput()->with('error', 'Unexpected error, please try again. code: ' . $validator->errors()->first()); } // Cari data berdasarkan ID $akun = Produk::find($id); // Jika data ditemukan if ($akun) { // Update data dengan data baru dari form yang telah dibersihkan $akun->name = $request->input('name'); $akun->stok = $request->input('stok'); $akun->price = $request->input('price'); // Simpan perubahan pada database $akun->save(); return redirect()->route('produk')->with('success', 'You are successfully added new records'); } else { return redirect()->route('produk')->with('error', 'Unexpected error'); } } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { // Cari data berdasarkan ID $post = Produk::find($id); // Jika data ditemukan if ($post) { // Hapus data dari database $post->delete(); return redirect()->route('produk')->with('success', 'You are successfully deleted records'); } else { return redirect()->route('produk')->with('error', 'Data not found'); } } public function addStockFromSupplier(Request $request) { $validator = Validator::make($request->all(), [ 'nama_supplier' => 'required', 'nama_produk' => 'required', 'jumlah' => 'required|integer', 'harga' => 'required|numeric' ]); if ($validator->fails()) { return redirect()->back()->withInput()->with('error', 'Unexpected error, please try again. code: ' . $validator->errors()->first()); } $input = $request->all(); $supplier = new Supplier([ 'nama_supplier' => $input['nama_supplier'], 'nama_produk' => $input['nama_produk'], 'jumlah' => $input['jumlah'], 'harga' => $input['harga'] ]); if ($supplier->save()) { $product = Produk::where('name', $input['nama_produk'])->first(); if ($product) { $product->stok += $input['jumlah']; $product->save(); return redirect()->route('produk')->with('success', 'Stock successfully added from supplier'); } else { return redirect()->route('produk')->with('error', 'Product not found'); } } else { return redirect()->route('produk')->with('error', 'An error occurred while saving supplier data'); } } }