Skip to content

Commit

Permalink
add nested menus
Browse files Browse the repository at this point in the history
  • Loading branch information
MoamenEltouny committed Mar 17, 2022
1 parent d3ab8c1 commit 74d09dd
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 18 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"providers": [
"Pharaonic\\Laravel\\Menus\\MenusServiceProvider"
]
}
},
"file": ["src/helper.php"]
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 1 addition & 4 deletions src/MenusServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ public function boot()

// Blade - Directive
Blade::directive('menu', function ($section) {
$section = Menu::section(trim($section, '\'"'))->get();
if ($section->isEmpty()) return;

return view('laravel-menus::section', ['section' => $section])->render();
return "<?php echo \menu($section, true); ?>";
});
}
}
60 changes: 48 additions & 12 deletions src/Models/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
* @property string $url
* @property integer $sort
* @property integer $visible
* @property integer $parent_id
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Menu $children
* @property MenuTranslation $translations
*
* @author Moamen Eltouny (Raggi) <[email protected]>
Expand All @@ -26,7 +28,7 @@ class Menu extends Model
*
* @var array
*/
protected $fillable = ['section', 'url', 'sort', 'visible'];
protected $fillable = ['section', 'url', 'sort', 'visible', 'parent_id'];

/**
* Translatable attributes names.
Expand All @@ -47,7 +49,7 @@ class Menu extends Model
];

/**
* Get section'items.
* Get section items.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $section
Expand All @@ -56,7 +58,34 @@ class Menu extends Model
*/
public function scopeSection($query, string $section, string $locale = null)
{
return $query->translated($locale)->where('section', $section)->where('visible', true)->orderBy('sort', 'ASC');
return $query->translated($locale)->with([
'children' => function ($q) {
return $q->visible();
},
'children.translations'
])->where([
'section' => $section,
'parent_id' => null
])->visible()->orderBy('sort', 'ASC');
}

/**
* Get visible items.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeVisible($query)
{
return $query->where('visible', true);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function children()
{
return $this->hasMany(Menu::class, 'parent_id', 'id');
}

/**
Expand All @@ -70,18 +99,25 @@ public function scopeSection($query, string $section, string $locale = null)
* @param string $locale
* @return Menu
*/
public static function set(string $section, string $title, string $url, int $sort = 0, bool $visible = true, string $locale = null)
public static function set(string $section, mixed $title, string $url, int $parent = null, int $sort = 0, bool $visible = true)
{
$menu = new self;
$menu->section = $section;
$menu->url = $url;
$menu->sort = $sort;
$menu->visible = $visible;
$menu->save();
$menu = new self;
$data = [
'section' => $section,
'url' => $url,
'parent_id' => $parent,
'sort' => $sort,
'visible' => $visible
];

$localKey = $menu->translationsKey ?? 'locale';

$menu->translateOrNew($locale ?? app()->getLocale())->title = $title;
$menu->save();
if (is_array($title))
$data[$localKey] = $title;
else
$data[$localKey][app()->getLocale()]['title'] = $title;

$menu->fill($data)->save();
return $menu;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public function up()

$table->integer('sort')->default(0);
$table->boolean('visible')->default(true);
$table->unsignedBigInteger('parent_id')->nullable();

$table->timestamps();
$table->foreign('parent_id')->references('id')->on('menus')->onDelete('set null');
});
}

Expand Down
15 changes: 15 additions & 0 deletions src/helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use Pharaonic\Laravel\Menus\Models\Menu;

if (!function_exists('menu')) {
function menu(string $section, bool $view = false)
{
$section = Menu::section(trim($section, '\'"'))->get();

if ($section->isEmpty()) return;
if (!$view) return $section;

return view('laravel-menus::section', ['section' => $section]);
}
}
11 changes: 10 additions & 1 deletion src/views/section.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<ul>
@foreach($section as $item)
<li><a href="{{ $item->url }}">{!! $item->title !!}</a></li>
<li>
<a href="{{ $item->url }}">{!! $item->translateOrDefault()->title ?? null !!}</a>
@if($item->children->isNotEmpty())
<ul>
@foreach($item->children as $sub)
<li><a href="{{ $sub->url }}">{!! $sub->translateOrDefault()->title ?? null !!}</a></li>
@endforeach
</ul>
@endif
</li>
@endforeach
</ul>

0 comments on commit 74d09dd

Please sign in to comment.