Skip to content

Commit

Permalink
Structuring Menu with hierarchy Model
Browse files Browse the repository at this point in the history
  • Loading branch information
hiamir committed Nov 15, 2022
1 parent 8e0aa5e commit 41cf6eb
Show file tree
Hide file tree
Showing 18 changed files with 346 additions and 25 deletions.
33 changes: 25 additions & 8 deletions app/Http/Livewire/Admin/Menu/MenuParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
namespace App\Http\Livewire\Admin\Menu;

use App\Models\Menu;
use App\Traits\Data;
use App\Traits\General;
use Illuminate\Database\Eloquent\Model;
use Livewire\Component;

class MenuParent extends Component
{
use General;
use Data;

public string $pageHeader = 'Menu';
public array $menu;
public ?int $rowID = null;
public ?int $menuID = null;

public $menuRecord = null;

public array $parentData = [];

protected $listeners = ['createMenu', 'editMenu', 'deleteMenu'];


Expand All @@ -37,38 +42,47 @@ public function resetForm()
'menu.name.required' => 'Menu name is required.',
'menu.name.min' => 'Menu must be at-least 4 letters long.',
'menu.name.unique' => ':attribute menu already exists!.',
'menu.parent_id.integer' => ':attribute must be integer.',
'menu.parent_id.gt' => ':attribute must be positive integer.',
];

protected function rules()
{
return [
'menu.name' => 'required|min:4|unique:menus,name,' . $this->rowID
'menu.name' => 'required|min:4|unique:menus,name,' . $this->rowID,
'menu.parent_id'=>'numeric|gt:0'
];
}

protected function validationAttributes()
{
return [
'menu.name' => $this->menu['name'],
'menu.menuID' => $this->menu['menuID'],
];
}

public function resetInput()
{
$this->menu = ['name' => ''];
$this->menu = ['name' => '','menuID'=>null];
}

protected function getRecord($row)
{
$this->rowID = $row['id'];
$this->menuRecord = Menu::find($row)->first();
$this->menuRecord = Menu::find($this->rowID)->first();
}


public function createMenu()
public function createMenu($id=null)
{


$this->resetForm();
if($id !== null) $this->menu['menuID']= $id;
$this->modelInfo('create', 'Menu');
$this->parentData = $this->get_array_for_select_input(Menu::select('id', 'name')->where('parent_id',null)->where('id',$id)->get());

$this->dispatchBrowserEvent('FirstModel', ['show' => true]);
}

Expand All @@ -88,12 +102,13 @@ public function deleteMenu($row)
$this->dispatchBrowserEvent('FirstModel', ['show' => true]);
}

protected function afterSave($formType){
protected function afterSave($formType)
{
$this->emit('refreshDatatable');
$this->resetForm();
$this->dispatchBrowserEvent('FirstModel', ['show' => false]);

switch($formType){
switch ($formType) {
case 'create':
$this->dispatchBrowserEvent('Toast', ['show' => true, 'type' => 'success', 'message' => "'" . $this->menuRecord->name . "'" . ' was added to Menu Level!']);
break;
Expand All @@ -112,20 +127,21 @@ protected function afterSave($formType){
public function submit()
{


switch ($this->formType) {

case 'create':
$this->validate();
$this->menuRecord = new Menu();
$this->menuRecord->name = $this->menu['name'];
($this->menu['menuID'] !== null) ? $this->menuRecord->parent_id = $this->menu['menuID'] : $this->menuRecord->parent_id=null;
$this->menuRecord->save();
$this->afterSave($this->formType);
break;

case 'update':
$this->validate();
$this->menuRecord->name = $this->menu['name'];
$this->menuRecord->menus_id = $this->menu['menuID'];
$this->menuRecord->save();
$this->afterSave($this->formType);
break;
Expand All @@ -141,6 +157,7 @@ public function submit()

public function render()
{
return view('livewire.admin.menu.menu-parent');
$menus = Menu::with('childMenus')->where('parent_id','=',null)->get();
return view('livewire.admin.menu.menu-parent',['menus'=>$menus]);
}
}
9 changes: 8 additions & 1 deletion app/Http/Livewire/Admin/Menu/MenuParentDatatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public function configure(): void
];
} elseif ($column->isField('name')) {
return [
'class' => '!w-[60%]',
'class' => '!w-[50%]',
];
}elseif ($column->isField('parent_id')) {
return [
'class' => '!w-[10%]',
];
} elseif ($column->isField('created_at')) {
return [
Expand Down Expand Up @@ -91,6 +95,9 @@ public function columns(): array
Column::make("Name", "name")
->searchable()
->sortable(),
Column::make("Belongs to", "parent_id")
->searchable()
->sortable(),
Column::make("Created at", "created_at")
->format(fn($value, $row, Column $column) => $row->created_at->diffForHumans())
->sortable(),
Expand Down
8 changes: 8 additions & 0 deletions app/Models/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

class Menu extends Model
{
use HasFactory;
// use HasRecursiveRelationships;
public function childMenus()
{
return $this->hasMany(Menu::class, 'parent_id', 'id')->with('childMenus');
}


}
8 changes: 8 additions & 0 deletions app/Traits/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ public static function uri_guard($request)
{
return explode('/', $request->getRequestUri())[1];
}

public function get_array_for_select_input($record){
$array=[];
foreach ($record as $data){
$array[$data->id]=$data->name;
}
return $array;
}
}
28 changes: 28 additions & 0 deletions app/View/Components/Item/Select.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\View\Components\Item;

use Illuminate\View\Component;

class Select extends Component
{
/**
* Create a new component instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('components.item.select');
}
}
28 changes: 28 additions & 0 deletions app/View/Components/Svg/Security.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\View\Components\Svg;

use Illuminate\View\Component;

class Security extends Component
{
/**
* Create a new component instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('components.svg.security');
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"laravel/sanctum": "^3.0",
"laravel/tinker": "^2.7",
"livewire/livewire": "^2.5",
"rappasoft/laravel-livewire-tables": "^2.8"
"rappasoft/laravel-livewire-tables": "^2.8",
"staudenmeir/laravel-adjacency-list": "^1.0"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",
Expand Down
116 changes: 115 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions database/migrations/2022_11_09_085026_create_menus_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function up()
$table->id();
$table->string('name')->unique();
$table->string('svg')->unique()->nullable();
$table->unsignedBigInteger('menu_levels_id');
$table->unsignedBigInteger('parent_id')->nullable();
$table->timestamps();

//FOREIGN KEYS
$table->foreign('menu_levels_id','menus_fk0')->on('menu_levels')->references('id')->onUpdate('cascade')->onDelete('cascade');
$table->foreign('parent_id','menus_fk0')->on('menus')->references('id')->onUpdate('cascade')->onDelete('cascade');
});
}

Expand All @@ -32,7 +32,7 @@ public function up()
*/
public function down()
{
Schema::table('menu_items', function (Blueprint $table) {
Schema::table('menus', function (Blueprint $table) {
$table->dropForeign('menus_fk0');
});
Schema::dropIfExists('menus');
Expand Down
Loading

0 comments on commit 41cf6eb

Please sign in to comment.