diff --git a/app/Http/Livewire/Admin/Menu/MenuParent.php b/app/Http/Livewire/Admin/Menu/MenuParent.php
index 58ba93b..bd2667b 100644
--- a/app/Http/Livewire/Admin/Menu/MenuParent.php
+++ b/app/Http/Livewire/Admin/Menu/MenuParent.php
@@ -19,7 +19,7 @@ class MenuParent extends Component
public string $pageHeader = 'Menu';
public array $menu, $guards, $routes, $parentData;
public ?int $rowID = null, $menuID = null;
- public $menus, $menuRecord = null;
+ public $menuRecord = null;
/* INITIALIZATION AT START */
public function mount()
@@ -32,9 +32,9 @@ public function resetForm()
$this->menu= $this->Repository(name: 'resetInputs');
$this->resetErrorBag();
$this->resetValidation();
-
}
+
/* VALIDATION MESSAGES */
protected array $messages = [
'menu.name.required' => 'Menu name is required.',
@@ -45,9 +45,10 @@ public function resetForm()
'menu.guard.required' => 'Guard is required.',
'menu.guard.integer' => ':attribute must be integer.',
'menu.guard.gt' => ':attribute must be positive integer.',
+ 'menu.svg.unique' => ':attribute already exists',
'menu.route.required' => 'Select a route for this menu.',
- 'menuItem.route.unique' => ':attribute already exists.',
- 'menuItem.route.regex' => ':attribute must lower case. Allowed ' . ' only',
+ 'menu.route.unique' => ':attribute already exists.',
+ 'menu.route.regex' => ':attribute is invalid',
'menu.sort.required' => 'Give te order for this menu',
'menu.sort.integer' => ':attribute must be integer.',
'menu.sort.gt' => ':attribute must be positive integer.',
@@ -60,7 +61,8 @@ protected function rules(): array
'menu.name' => 'required|min:4|unique:menus,name,' . $this->rowID,
'menu.menuID' => 'numeric|gt:0|nullable',
'menu.guard' => 'required|numeric|gt:0',
- 'menu.route' => 'required|min:4|regex:/^[a-z,\.-]+$/|unique:menus,route,' . $this->rowID,
+// 'menu.svg' => 'unique:menus,svg,' . $this->rowID,
+// 'menu.route' => '|unique:menus,route,' . $this->rowID,
'menu.sort' => 'required|numeric|gt:0'
];
}
@@ -70,7 +72,9 @@ protected function validationAttributes(): array
{
return [
'menu.name' => $this->menu['name'],
+ 'menu.route' => 'Route',
'menu.menuID' => $this->menu['menuID'],
+ 'menu.svg' => $this->menu['svg'],
];
}
@@ -156,7 +160,7 @@ public function render()
if (count($this->guards) === 1) $this->menu['guard'] = array_key_first($this->guards);
}
- $this->menus = Menu::with('childMenus')->where('parent_id', '=', null)->get();
- return view('livewire.admin.menu.menu-parent', ['menus' => $this->menus]);
+ $menus = Menu::with('childMenus')->where('parent_id', '=', null)->orderBy('sort','ASC')->get();
+ return view('livewire.admin.menu.menu-parent', ['menus' => $menus]);
}
}
diff --git a/app/Http/Livewire/Layout/Sidebar.php b/app/Http/Livewire/Layout/Sidebar.php
index 2f050f3..0923a82 100644
--- a/app/Http/Livewire/Layout/Sidebar.php
+++ b/app/Http/Livewire/Layout/Sidebar.php
@@ -10,15 +10,24 @@
class Sidebar extends Component
{
protected $listeners=['refreshSidebar'=>'$refresh'];
+public array $routes=['hello'];
-
- public function mount()
+ public function getRoutes($menu):array
{
+ $array=[];
+ array_push($array,$menu->route);
+ foreach ($menu->childMenus as $childMenu1){
+ array_push($array,$childMenu1->route);
+ foreach ($childMenu1->childMenus as $childMenu2){
+ array_push($array,$childMenu2->route);
+ }
+ }
+ return $array;
}
public function render()
{
- $sideBar=Menu::with('childMenus')->where('parent_id',null)->get();
+ $sideBar=Menu::with('childMenus')->where('parent_id',null)->orderBy('sort','ASC')->get();
return view('livewire.layout.sidebar', ['sideBar' => $sideBar]);
}
}
diff --git a/app/Repositories/MenuRepository.php b/app/Repositories/MenuRepository.php
index d6988b1..232ee67 100644
--- a/app/Repositories/MenuRepository.php
+++ b/app/Repositories/MenuRepository.php
@@ -13,8 +13,9 @@ class MenuRepository implements MenuRepositoryInterface
use Data;
/* RESET FORM FIELDS */
- public function resetInputs(): array{
- return ['name' => null,'svg' => null, 'menuID' => null, 'guard' => null, 'route' => null, 'sort' => null];
+ public function resetInputs(): array
+ {
+ return ['name' => null, 'svg' => null, 'menuID' => null, 'guard' => null, 'route' => null, 'sort' => null];
}
/* GET RECORD */
@@ -63,8 +64,8 @@ public function getParentData($type, $record, $level): array
case 'update':
return match ($level) {
- 'l1' => $this->get_array_for_select_input($this->getRecordByField(['id', 'name'], 'parent_id', null)),
- 'l2' => $this->get_second_array_for_select_input($this->getRecordByField(['id', 'name'], 'parent_id', null)),
+ 'l1' => $this->get_all_parent_array_for_select_input($this->getRecordByField(['id', 'name'], 'parent_id', null),$record->id),
+ 'l2' => $this->get_all_parent_array_for_select_input($this->getRecordByField(['id', 'name'], 'parent_id', null)),
default => $this->get_array_for_select_input($this->getRecordByField(['id', 'name'], 'parent_id', null)),
};
@@ -77,51 +78,66 @@ public function getParentData($type, $record, $level): array
/* GET ROUTE DATA ARRAY */
public function getRoutes($type, $record): array
{
- return match ($type) {
- 'create' => Data::get_routes_array_for_select_input(),
- 'update' => Data::get_routes_array_for_select_input([$record->route]),
- default => [],
- };
+
+ switch ($type) {
+ case 'create':
+ $arr = Data::get_routes_array_for_select_input();
+ return (array_merge(["none" => "None"], $arr));
+ case 'update':
+ $arr = Data::get_routes_array_for_select_input([$record->route]);
+ return (array_merge(["none" => "None"], $arr));
+ default:
+ return [];
+
+ }
+// $arr = Data::get_routes_array_for_select_input([$record->route]);
+// $arr = (array_merge(["none" => "None"], $arr));
+// return match ($type) {
+// 'create' => Data::get_routes_array_for_select_input(),
+// 'update' => $arr,
+// default => [],
+// };
+
}
/* ASSIGN DATA FOR SUBMIT */
public function assignData($record, $formInputs): Menu
{
$record->name = ucfirst($formInputs['name']);
- $record->svg = strtolower($formInputs['svg']);
+ $record->svg = ($formInputs['svg'] === '') ? null : strtolower($formInputs['svg']);
$record->parent_id = $formInputs['menuID'];
- $record->route = $formInputs['route'];
+ $record->route = ($formInputs['route'] === 'none') ? 'none' : $formInputs['route'];
$record->guards_id = $formInputs['guard'];
$record->sort = $formInputs['sort'];
return $record;
}
/* SAVE DATA FOR SUBMIT */
- public function saveData($record,$formInputs):array
+ public function saveData($record, $formInputs): array
{
try {
- $success= DB::transaction(function () use ($record,$formInputs) {
- $this->assignData($record,$formInputs)->save();
- return [true, $record];
+ $success = DB::transaction(function () use ($record, $formInputs) {
+ $this->assignData($record, $formInputs)->save();
+ return [true, $record];
});
} catch (\Exception $e) {
DB::rollback();
- return [false, $e->getMessage()];
+ return [false, $e->getMessage()];
}
- return $success;
+ return $success;
}
/* DELETE DATA FOR SUBMIT */
public function deleteData($record): array
{
try {
- $success= DB::transaction(function () use ($record) {
+ $success = DB::transaction(function () use ($record) {
$record->delete();
- return [true, $record];
+ return [true, $record];
});
} catch (\Exception $e) {
DB::rollback();
- return [false, $e->getMessage()];
+ return [false, $e->getMessage()];
}
return $success;
}
diff --git a/app/Traits/Data.php b/app/Traits/Data.php
index 476b6b5..5a0975b 100644
--- a/app/Traits/Data.php
+++ b/app/Traits/Data.php
@@ -34,7 +34,22 @@ public function get_array_for_select_input($record): array
{
$array = [];
foreach ($record as $data) {
- $array[$data->id] = $data->name;
+ $array[$data->id] = $data->name.' (Parent)';
+ }
+ return $array;
+ }
+
+ /* GET ALL PARENTS FOR SELECT OPTIONS */
+ public function get_all_parent_array_for_select_input($record,$exclude=null): array
+ {
+ $array = [];
+ foreach ($record as $data) {
+ $array[$data->id] = $data->name.' (Parent)';
+ foreach ($data->childMenus as $childData) {
+ if($childData->id !== $exclude) {
+ $array[$childData->id] = $childData->name . ' (Child)';
+ }
+ }
}
return $array;
}
diff --git a/database/migrations/2022_11_09_085026_create_menus_table.php b/database/migrations/2022_11_09_085026_create_menus_table.php
index 521de03..7dfcbe9 100644
--- a/database/migrations/2022_11_09_085026_create_menus_table.php
+++ b/database/migrations/2022_11_09_085026_create_menus_table.php
@@ -17,8 +17,8 @@ public function up()
$table->id();
$table->string('name')->unique();
$table->unsignedBigInteger('guards_id');
- $table->string('route')->unique();
- $table->string('svg')->unique()->nullable();
+ $table->string('route')->nullable();
+ $table->string('svg')->nullable();
$table->integer('sort');
$table->unsignedBigInteger('permissions_id')->nullable();
$table->unsignedBigInteger('parent_id')->nullable();
diff --git a/resources/views/components/svg/cog.blade.php b/resources/views/components/svg/cog.blade.php
new file mode 100644
index 0000000..799fdb4
--- /dev/null
+++ b/resources/views/components/svg/cog.blade.php
@@ -0,0 +1,4 @@
+
diff --git a/resources/views/components/svg/gear.blade.php b/resources/views/components/svg/gear.blade.php
index b347f64..9003ced 100644
--- a/resources/views/components/svg/gear.blade.php
+++ b/resources/views/components/svg/gear.blade.php
@@ -1,3 +1,3 @@
-