-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3ab8c1
commit 74d09dd
Showing
6 changed files
with
78 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
|
@@ -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. | ||
|
@@ -47,7 +49,7 @@ class Menu extends Model | |
]; | ||
|
||
/** | ||
* Get section'items. | ||
* Get section items. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param string $section | ||
|
@@ -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'); | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |