127 lines
4.0 KiB
PHP
127 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admins\JenisMenu;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\KategoriProduk;
|
|
use App\Models\KelompokKategori;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class JenisMenuController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$kategori_produks = KategoriProduk::get();
|
|
|
|
return view('pages.admin.jenis_menu.index', compact('kategori_produks'));
|
|
}
|
|
|
|
public function getDataMenu()
|
|
{
|
|
$data = KelompokKategori::with(['kategoriProduk'])->get();
|
|
$nomor = 1;
|
|
|
|
return $datatables = datatables()
|
|
->of($data)
|
|
->addColumn('nomor', function ($data) use (&$nomor) {
|
|
return $nomor++;
|
|
})
|
|
->addColumn('kategori_produk', function ($data) {
|
|
return $data->kategoriProduk->nama_kategori_produk;
|
|
})
|
|
->addColumn('ubah', function ($data) {
|
|
return '<div class="btn-group">
|
|
<a href="javascript:void(0)" onclick="editJenisMenu(\'' . $data->id . '\')">
|
|
<span class="btn btn-md btn-warning"><i class="fas fa-edit"></i> Edit</span>
|
|
</a>
|
|
</div>';
|
|
})
|
|
->rawColumns(['nomor', 'kode_kelompok_kategori', 'nama_kelompok_kategori', 'ubah'])
|
|
->make(true);
|
|
}
|
|
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
// validation
|
|
$rules = array(
|
|
'kategori_produk' => 'required',
|
|
'kode_kelompok_kategori' => 'required',
|
|
'nama_kelompok_kategori' => 'required',
|
|
);
|
|
$validator = Validator::make($request->all(), $rules);
|
|
|
|
// check validation
|
|
if ($validator->fails()) {
|
|
// If validation fails, return with errors
|
|
return response()->json(['errors' => $validator->errors()], 422);
|
|
} else {
|
|
// create product
|
|
$kemlompokKategori = KelompokKategori::create([
|
|
'kategori_produk_id' => $request->kategori_produk,
|
|
'kode_kelompok_kategori' => $request->kode_kelompok_kategori,
|
|
'nama_kelompok_kategori' => $request->nama_kelompok_kategori,
|
|
'created_at' => Carbon::now()
|
|
]);
|
|
|
|
// Return a success response
|
|
return response()->json(['message' => 'Data created successfully']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id)
|
|
{
|
|
$data = KelompokKategori::with(['kategoriProduk'])->findOrFail($id);
|
|
return response()->json($data);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, string $id)
|
|
{
|
|
// validation
|
|
$rules = array(
|
|
'kategori_produk' => 'required',
|
|
'kode_kelompok_kategori' => 'required',
|
|
'nama_kelompok_kategori' => 'required',
|
|
);
|
|
$validator = Validator::make($request->all(), $rules);
|
|
|
|
// check validation
|
|
if ($validator->fails()) {
|
|
// If validation fails, return with errors
|
|
return response()->json(['errors' => $validator->errors()], 422);
|
|
} else {
|
|
$kelompokKategori = KelompokKategori::findOrFail($id);
|
|
|
|
// update produk
|
|
$kelompokKategori->update([
|
|
'kategori_produk_id' => $request->kategori_produk,
|
|
'kode_kelompok_kategori' => $request->kode_kelompok_kategori,
|
|
'nama_kelompok_kategori' => $request->nama_kelompok_kategori,
|
|
'created_at' => Carbon::now()
|
|
]);
|
|
|
|
// Return a success response
|
|
return response()->json(['message' => 'Data updated successfully']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
//
|
|
}
|
|
}
|