fix redirect back with params menu id
This commit is contained in:
@@ -6,7 +6,6 @@ use App\Http\Requests\MenuRequest;
|
||||
use App\Models\Menu;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class MenusController extends Controller
|
||||
{
|
||||
@@ -15,36 +14,33 @@ class MenusController extends Controller
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$menuId = $request->query('menu_id');
|
||||
$user = Auth::user();
|
||||
$userId = $user->id;
|
||||
$menuId = (int) $request->query('menu_id', 0);
|
||||
$permissions = $this->permissions[$menuId] ?? []; // Avoid undefined index error
|
||||
|
||||
// Ambil role_id yang dimiliki user
|
||||
$roleIds = DB::table('user_role')
|
||||
->where('user_id', $userId)
|
||||
->pluck('role_id');
|
||||
$creator = $permissions['allow_create'] ?? 0;
|
||||
$updater = $permissions['allow_update'] ?? 0;
|
||||
$destroyer = $permissions['allow_destroy'] ?? 0;
|
||||
|
||||
// Ambil data akses berdasarkan role_id dan menu_id
|
||||
$roleAccess = DB::table('role_menu')
|
||||
->whereIn('role_id', $roleIds)
|
||||
->where('menu_id', $menuId)
|
||||
->first();
|
||||
|
||||
// Pastikan roleAccess tidak null sebelum mengakses properti
|
||||
$creator = $roleAccess->allow_create ?? 0;
|
||||
$updater = $roleAccess->allow_update ?? 0;
|
||||
$destroyer = $roleAccess->allow_destroy ?? 0;
|
||||
|
||||
return view('menus.index', compact('creator', 'updater', 'destroyer'));
|
||||
return view('menus.index', compact('creator', 'updater', 'destroyer', 'menuId'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
public function create(Request $request)
|
||||
{
|
||||
$parent_menus = Menu::whereNull('parent_id')->get();
|
||||
return view("menus.create", compact('parent_menus'));
|
||||
$menuId = $request->query('menu_id'); // Get menu_id from request
|
||||
$menu = Menu::with('children')->find($menuId); // Find the menu
|
||||
|
||||
// Get IDs of all child menus to exclude
|
||||
$excludedIds = $menu ? $this->getChildMenuIds($menu) : [$menuId];
|
||||
|
||||
// Fetch only menus that have children and are not in the excluded list
|
||||
$parent_menus = Menu::whereHas('children')
|
||||
->whereNotIn('id', $excludedIds)
|
||||
->get();
|
||||
|
||||
return view("menus.create", compact('parent_menus', 'menuId'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,11 +73,16 @@ class MenusController extends Controller
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(string $id)
|
||||
public function edit(string $id, Request $request)
|
||||
{
|
||||
$menu = Menu::findOrFail($id);
|
||||
$parent_menus = Menu::whereNull('parent_id')->where('id','!=',$id)->get();
|
||||
return view("menus.edit", compact('menu','parent_menus'));
|
||||
$menuId = $request->query('menu_id');
|
||||
$menu = Menu::with('children')->find($id);
|
||||
$excludedIds = $menu ? $this->getChildMenuIds($menu) : [$id];
|
||||
|
||||
$parent_menus = Menu::whereHas('children')
|
||||
->whereNotIn('id', $excludedIds)
|
||||
->get();
|
||||
return view("menus.edit", compact('menu','parent_menus', 'menuId'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,4 +132,15 @@ class MenusController extends Controller
|
||||
$child->delete();
|
||||
}
|
||||
}
|
||||
|
||||
private function getChildMenuIds($menu)
|
||||
{
|
||||
$ids = [$menu->id]; // Start with current menu ID
|
||||
|
||||
foreach ($menu->children as $child) {
|
||||
$ids = array_merge($ids, $this->getChildMenuIds($child)); // Recursively fetch children
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ class CreateMenu {
|
||||
initCreateMenu() {
|
||||
const toastNotification = document.getElementById("toastNotification");
|
||||
const toast = new bootstrap.Toast(toastNotification);
|
||||
let menuId = document.getElementById("menuId").value;
|
||||
document
|
||||
.getElementById("btnCreateMenus")
|
||||
.addEventListener("click", async function () {
|
||||
@@ -41,7 +42,7 @@ class CreateMenu {
|
||||
result.message;
|
||||
toast.show();
|
||||
setTimeout(() => {
|
||||
window.location.href = "/menus";
|
||||
window.location.href = `/menus?menu_id=${menuId}`;
|
||||
}, 2000);
|
||||
} else {
|
||||
let error = await response.json();
|
||||
|
||||
@@ -31,6 +31,7 @@ class Menus {
|
||||
tableContainer.innerHTML = "";
|
||||
let canUpdate = tableContainer.getAttribute("data-updater") === "1";
|
||||
let canDelete = tableContainer.getAttribute("data-destroyer") === "1";
|
||||
let menuId = tableContainer.getAttribute("data-menuId");
|
||||
|
||||
this.table = new Grid({
|
||||
columns: [
|
||||
@@ -47,7 +48,7 @@ class Menus {
|
||||
|
||||
if (canUpdate) {
|
||||
buttons += `
|
||||
<a href="/menus/${cell}/edit" class="btn btn-yellow btn-sm d-inline-flex align-items-center justify-content-center">
|
||||
<a href="/menus/${cell}/edit?menu_id=${menuId}" class="btn btn-yellow btn-sm d-inline-flex align-items-center justify-content-center">
|
||||
<i class='bx bx-edit'></i>
|
||||
</a>
|
||||
`;
|
||||
|
||||
@@ -6,6 +6,7 @@ class UpdateMenu {
|
||||
initUpdateMenu() {
|
||||
const toastNotification = document.getElementById("toastNotification");
|
||||
const toast = new bootstrap.Toast(toastNotification);
|
||||
let menuId = document.getElementById("menuId").value;
|
||||
document
|
||||
.getElementById("btnUpdateMenus")
|
||||
.addEventListener("click", async function () {
|
||||
@@ -41,7 +42,7 @@ class UpdateMenu {
|
||||
result.message;
|
||||
toast.show();
|
||||
setTimeout(() => {
|
||||
window.location.href = "/menus";
|
||||
window.location.href = `/menus?menu_id=${menuId}`;
|
||||
}, 2000);
|
||||
} else {
|
||||
let error = await response.json();
|
||||
|
||||
@@ -5,11 +5,12 @@
|
||||
@include('layouts.partials/page-title', ['title' => 'Settings', 'subtitle' => 'Menu'])
|
||||
|
||||
<x-toast-notification />
|
||||
<input type="hidden" id="menuId" value="{{ $menuId ?? 0 }}">
|
||||
<div class="row d-flex justify-content-center">
|
||||
<div class="col-md-6">
|
||||
<div class="card">
|
||||
<div class="card-header d-flex justify-content-end">
|
||||
<a href="{{ route('menus.index') }}" class="btn btn-sm btn-secondary">Back</a>
|
||||
<a href="{{ route('menus.index', ['menu_id' => $menuId ?? 0]) }}" class="btn btn-sm btn-secondary">Back</a>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form id="formCreateMenus" action="{{route("api.menus.store")}}" method="post">
|
||||
|
||||
@@ -9,11 +9,12 @@
|
||||
@include('layouts.partials/page-title', ['title' => 'Settings', 'subtitle' => 'Menu'])
|
||||
|
||||
<x-toast-notification/>
|
||||
<input type="hidden" id="menuId" value="{{ $menuId ?? 0 }}">
|
||||
<div class="row d-flex justify-content-center">
|
||||
<div class="col-md-6">
|
||||
<div class="card">
|
||||
<div class="card-header d-flex justify-content-end">
|
||||
<a href="{{ route('menus.index') }}" class="btn btn-sm btn-secondary">Back</a>
|
||||
<a href="{{ route('menus.index', ['menu_id' => $menuId]) }}" class="btn btn-sm btn-secondary">Back</a>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form id="formUpdateMenus" action="{{route("api.menus.update", $menu->id)}}" method="post">
|
||||
|
||||
@@ -17,13 +17,14 @@
|
||||
<div class="card-body">
|
||||
<div class="d-flex flex-wrap justify-content-end align-items-center mb-2">
|
||||
@if ($creator)
|
||||
<a href="{{ route('menus.create')}}" class="btn btn-success btn-sm d-block d-sm-inline w-auto">Create</a>
|
||||
<a href="{{ route('menus.create', ['menu_id' => $menuId])}}" class="btn btn-success btn-sm d-block d-sm-inline w-auto">Create</a>
|
||||
@endif
|
||||
</div>
|
||||
<div>
|
||||
<div id="table-menus"
|
||||
data-updater="{{ $updater }}"
|
||||
data-destroyer="{{ $destroyer }}">
|
||||
data-destroyer="{{ $destroyer }}"
|
||||
data-menuId="{{ $menuId }}">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user