-
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
0 parents
commit 830565d
Showing
8 changed files
with
316 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<p align="center"><a href="https://pharaonic.io" target="_blank"><img src="https://raw.githubusercontent.com/Pharaonic/logos/main/menus.jpg"></a></p> | ||
|
||
<p align="center"> | ||
<a href="https://php.net" target="_blank"><img src="https://img.shields.io/static/v1?label=PHP&message=%3E=7.2&color=blue&style=flat-square" alt="PHP Version : >= 7.2"></a> | ||
<a href="https://laravel.com" target="_blank"><img src="https://img.shields.io/static/v1?label=Laravel&message=%3E=6.0&color=F05340&style=flat-square" alt="Laravel Version : >= 6.0"></a> | ||
<img src="https://img.shields.io/static/v1?label=License&message=MIT&color=brightgreen&style=flat-square" alt="License"> | ||
<a href="https://liberapay.com/Pharaonic" target="_blank"><img src="https://img.shields.io/liberapay/receives/Pharaonic?color=gold&label=Support&style=flat-square" alt="Support"></a> | ||
<br> | ||
<a href="https://packagist.org/packages/Pharaonic/laravel-menus" target="_blank"><img src="https://img.shields.io/static/v1?label=Packagist&message=pharaonic/laravel-menus&color=blue&logo=packagist&logoColor=white" alt="Source"></a> | ||
<a href="https://packagist.org/packages/pharaonic/laravel-menus" target="_blank"><img src="https://poser.pugx.org/pharaonic/laravel-menus/v" alt="Packagist Version"></a> | ||
<a href="https://packagist.org/packages/pharaonic/laravel-menus" target="_blank"><img src="https://poser.pugx.org/pharaonic/laravel-menus/downloads" alt="Packagist Downloads"></a> | ||
</p> | ||
|
||
<h3 align="center">Laravel Menus Management.</h3> | ||
<h5 align="center">Depends on <a href="https://pharaonic.io/package/2-laravel/16-translatable" target="_blank">Translatable</a>.</h5> | ||
<br> | ||
|
||
## Documentation | ||
|
||
You can find the detailed documentation here in [Laravel Menus Documentation](https://pharaonic.io/package/2-laravel/21-menus). | ||
|
||
## Contributing | ||
|
||
Thank you for considering contributing to this package! Be one of Pharaonic team. | ||
|
||
## Pharaonic Sponsors | ||
|
||
We would like to extend our thanks to the following sponsors for funding Pharaonic packages development. If you are interested in becoming a sponsor, please visit the Pharaonic [Liberapay page](https://en.liberapay.com/Pharaonic) or [Patreon page](https://patreon.com/Pharaonic). | ||
|
||
## License | ||
|
||
This package is an open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). |
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,40 @@ | ||
{ | ||
"name": "pharaonic/laravel-menus", | ||
"description": "Laravel Menus Management.", | ||
"keywords": [ | ||
"laravel menus", | ||
"php menus", | ||
"menus", | ||
"laravel", | ||
"php" | ||
], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Moamen Eltouny (Raggi)", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.2", | ||
"laravel/framework": ">=6.0", | ||
"pharaonic/laravel-translatable": ">=1.0" | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Pharaonic\\Laravel\\Menus\\MenusServiceProvider" | ||
] | ||
} | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Pharaonic\\Laravel\\Menus\\": "src" | ||
} | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true | ||
} |
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,48 @@ | ||
<?php | ||
|
||
namespace Pharaonic\Laravel\Menus; | ||
|
||
use Illuminate\Support\Facades\Blade; | ||
use Illuminate\Support\ServiceProvider; | ||
use Pharaonic\Laravel\Menus\Models\Menu; | ||
|
||
class MenusServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
// Migration Loading | ||
$this->loadMigrationsFrom(__DIR__ . '/database/migrations'); | ||
} | ||
|
||
/** | ||
* Bootstrap services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
// Views | ||
$this->loadViewsFrom(__DIR__ . '/views', 'laravel-menus'); | ||
|
||
// Publishes | ||
$this->publishes([ | ||
__DIR__ . '/views' => resource_path('views/vendor/laravel-menus'), | ||
|
||
__DIR__ . '/database/migrations/2021_02_01_000016_create_menus_table.php' => database_path('migrations/2021_02_01_000016_create_menus_table.php'), | ||
__DIR__ . '/database/migrations/2021_02_01_000017_create_menu_translations_table.php' => database_path('migrations/2021_02_01_000017_create_menu_translations_table.php'), | ||
], ['pharaonic', 'laravel-menus']); | ||
|
||
// 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(); | ||
}); | ||
} | ||
} |
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,87 @@ | ||
<?php | ||
|
||
namespace Pharaonic\Laravel\Menus\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Pharaonic\Laravel\Translatable\Translatable; | ||
|
||
/** | ||
* @property integer $id | ||
* @property string $section | ||
* @property string $url | ||
* @property integer $sort | ||
* @property integer $visible | ||
* @property Carbon $created_at | ||
* @property Carbon $updated_at | ||
* @property MenuTranslation $translations | ||
* | ||
* @author Moamen Eltouny (Raggi) <[email protected]> | ||
*/ | ||
class Menu extends Model | ||
{ | ||
use Translatable; | ||
|
||
/** | ||
* Fields List | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = ['section', 'url', 'sort', 'visible']; | ||
|
||
/** | ||
* Translatable attributes names. | ||
* | ||
* @var array | ||
*/ | ||
protected $translatableAttributes = ['title']; | ||
|
||
/** | ||
* The attributes that should be cast. | ||
* | ||
* @var array | ||
*/ | ||
protected $casts = [ | ||
'data' => 'array', | ||
'sort' => 'integer', | ||
'visible' => 'boolean', | ||
]; | ||
|
||
/** | ||
* Get section'items. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param string $section | ||
* @param string|null $locale | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function scopeSection($query, string $section, string $locale = null) | ||
{ | ||
return $query->translated($locale)->where('section', $section)->where('visible', true)->orderBy('sort', 'ASC'); | ||
} | ||
|
||
/** | ||
* Create a localized menu. | ||
* | ||
* @param string $section | ||
* @param string $title | ||
* @param string $url | ||
* @param integer $sort | ||
* @param boolean $visible | ||
* @param string $locale | ||
* @return Menu | ||
*/ | ||
public static function create(string $section, string $title, string $url, int $sort = 0, bool $visible = true, string $locale = null) | ||
{ | ||
$menu = new self; | ||
$menu->section = $section; | ||
$menu->url = $url; | ||
$menu->sort = $sort; | ||
$menu->visible = $visible; | ||
$menu->save(); | ||
|
||
$menu->translateOrNew($locale ?? app()->getLocale())->title = $title; | ||
$menu->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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Pharaonic\Laravel\Menus\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
/** | ||
* @property integer $id | ||
* @property integer $menu_id | ||
* @property string $locale | ||
* @property string $title | ||
* | ||
* @author Moamen Eltouny (Raggi) <[email protected]> | ||
*/ | ||
class MenuTranslation extends Model | ||
{ | ||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = ['locale', 'menu_id', 'title']; | ||
|
||
/** | ||
* Indicates if the model should be timestamped. | ||
* | ||
* @var bool | ||
*/ | ||
public $timestamps = false; | ||
} |
38 changes: 38 additions & 0 deletions
38
src/database/migrations/2021_02_01_000016_create_menus_table.php
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,38 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateMenusTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('menus', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
|
||
$table->string('section', 40); | ||
$table->string('url'); | ||
|
||
$table->integer('sort')->default(0); | ||
$table->boolean('visible')->default(true); | ||
|
||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('menus'); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/database/migrations/2021_02_01_000017_create_menu_translations_table.php
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,36 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateMenuTranslationsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('menu_translations', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->string('title'); | ||
|
||
$table->string('locale')->index(); | ||
$table->unsignedBigInteger('menu_id'); | ||
$table->unique(['menu_id', 'locale']); | ||
$table->foreign('menu_id')->references('id')->on('menus')->onDelete('cascade'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('menu_translations'); | ||
} | ||
} |
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,5 @@ | ||
<ul> | ||
@foreach($section as $item) | ||
<li><a href="{{ $item->url }}">{!! $item->title !!}</a></li> | ||
@endforeach | ||
</ul> |