147 lines
4.4 KiB
PHP
Executable File
147 lines
4.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Category;
|
|
use App\Models\Menu;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Yajra\DataTables\DataTables;
|
|
|
|
class CategoryController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$menu = Menu::where('link', 'category.index')->first();
|
|
abort_if(Gate::denies('view', $menu), 403, 'Unauthorized User');
|
|
|
|
if ($request->ajax()) {
|
|
$data = Category::all();
|
|
return DataTables::of($data)->addIndexColumn()
|
|
->addColumn('action', function($row) use ($menu) {
|
|
$btn = '';
|
|
|
|
if(Auth::user()->can('delete', $menu)) {
|
|
$btn .= '<button class="btn btn-danger btn-sm btn-bold" data-action="'. route('category.destroy', $row->id) .'" id="destroyCategory'. $row->id .'" onclick="destroyCategory('. $row->id .')"> Hapus </button>';
|
|
}
|
|
|
|
if(Auth::user()->can('update', $menu)) {
|
|
$btn .= '<button class="btn btn-warning btn-sm btn-bold" id="editCategory'. $row->id .'" data-url="'. route('category.edit', $row->id) .'" data-action="'. route('category.update', $row->id) .'" onclick="editCategory('. $row->id .')"> Edit </button>';
|
|
}
|
|
|
|
return $btn;
|
|
})
|
|
->rawColumns(['action'])
|
|
->make(true);
|
|
}
|
|
return view('back.master.category');
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$menu = Menu::where('link', 'category.index')->first();
|
|
abort_if(Gate::denies('create', $menu), 403, 'Unauthorized User');
|
|
Category::create($request->all());
|
|
|
|
$response = [
|
|
"status" => 200,
|
|
"message" => "Data created successfully"
|
|
];
|
|
|
|
return response()->json($response);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param \App\Models\Category $category
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show(Category $category)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Category $category
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit(Category $category)
|
|
{
|
|
$menu = Menu::where('link', 'category.index')->first();
|
|
abort_if(Gate::denies('view', $menu), 403, 'Unauthorized User');
|
|
$category = Category::find($category->id);
|
|
$response = [
|
|
'data' => $category,
|
|
'status' => 200,
|
|
'message' => 'get data successfully'
|
|
];
|
|
return response()->json($response);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \App\Models\Category $category
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, Category $category)
|
|
{
|
|
$menu = Menu::where('link', 'category.index')->first();
|
|
abort_if(Gate::denies('update', $menu), 403, 'Unauthorized User');
|
|
Category::find($category->id)->update($request->all());
|
|
|
|
$response = [
|
|
"status" => 200,
|
|
"message" => "Data updated successfully"
|
|
];
|
|
|
|
return response()->json($response);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Category $category
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy(Category $category)
|
|
{
|
|
$menu = Menu::where('link', 'category.index')->first();
|
|
abort_if(Gate::denies('delete', $menu), 403, 'Unauthorized User');
|
|
Category::destroy($category->id);
|
|
|
|
$response = [
|
|
"status" => 200,
|
|
"message" => "Data deleted successfully"
|
|
];
|
|
|
|
return response()->json($response);
|
|
}
|
|
}
|