Skip to content

Commit 830565d

Browse files
author
Moamen Eltouny
committed
first commit
0 parents  commit 830565d

File tree

8 files changed

+316
-0
lines changed

8 files changed

+316
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<p align="center"><a href="https://pharaonic.io" target="_blank"><img src="https://raw.githubusercontent.com/Pharaonic/logos/main/menus.jpg"></a></p>
2+
3+
<p align="center">
4+
<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>
5+
<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>
6+
<img src="https://img.shields.io/static/v1?label=License&message=MIT&color=brightgreen&style=flat-square" alt="License">
7+
<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>
8+
<br>
9+
<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>
10+
<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>
11+
<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>
12+
</p>
13+
14+
<h3 align="center">Laravel Menus Management.</h3>
15+
<h5 align="center">Depends on <a href="https://pharaonic.io/package/2-laravel/16-translatable" target="_blank">Translatable</a>.</h5>
16+
<br>
17+
18+
## Documentation
19+
20+
You can find the detailed documentation here in [Laravel Menus Documentation](https://pharaonic.io/package/2-laravel/21-menus).
21+
22+
## Contributing
23+
24+
Thank you for considering contributing to this package! Be one of Pharaonic team.
25+
26+
## Pharaonic Sponsors
27+
28+
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).
29+
30+
## License
31+
32+
This package is an open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

composer.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "pharaonic/laravel-menus",
3+
"description": "Laravel Menus Management.",
4+
"keywords": [
5+
"laravel menus",
6+
"php menus",
7+
"menus",
8+
"laravel",
9+
"php"
10+
],
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Moamen Eltouny (Raggi)",
15+
"email": "raggi@raggitech.com"
16+
}
17+
],
18+
"require": {
19+
"php": ">=7.2",
20+
"laravel/framework": ">=6.0",
21+
"pharaonic/laravel-translatable": ">=1.0"
22+
},
23+
"config": {
24+
"sort-packages": true
25+
},
26+
"extra": {
27+
"laravel": {
28+
"providers": [
29+
"Pharaonic\\Laravel\\Menus\\MenusServiceProvider"
30+
]
31+
}
32+
},
33+
"autoload": {
34+
"psr-4": {
35+
"Pharaonic\\Laravel\\Menus\\": "src"
36+
}
37+
},
38+
"minimum-stability": "dev",
39+
"prefer-stable": true
40+
}

src/MenusServiceProvider.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Pharaonic\Laravel\Menus;
4+
5+
use Illuminate\Support\Facades\Blade;
6+
use Illuminate\Support\ServiceProvider;
7+
use Pharaonic\Laravel\Menus\Models\Menu;
8+
9+
class MenusServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* Register services.
13+
*
14+
* @return void
15+
*/
16+
public function register()
17+
{
18+
// Migration Loading
19+
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
20+
}
21+
22+
/**
23+
* Bootstrap services.
24+
*
25+
* @return void
26+
*/
27+
public function boot()
28+
{
29+
// Views
30+
$this->loadViewsFrom(__DIR__ . '/views', 'laravel-menus');
31+
32+
// Publishes
33+
$this->publishes([
34+
__DIR__ . '/views' => resource_path('views/vendor/laravel-menus'),
35+
36+
__DIR__ . '/database/migrations/2021_02_01_000016_create_menus_table.php' => database_path('migrations/2021_02_01_000016_create_menus_table.php'),
37+
__DIR__ . '/database/migrations/2021_02_01_000017_create_menu_translations_table.php' => database_path('migrations/2021_02_01_000017_create_menu_translations_table.php'),
38+
], ['pharaonic', 'laravel-menus']);
39+
40+
// Blade - Directive
41+
Blade::directive('menu', function ($section) {
42+
$section = Menu::section(trim($section, '\'"'))->get();
43+
if ($section->isEmpty()) return;
44+
45+
return view('laravel-menus::section', ['section' => $section])->render();
46+
});
47+
}
48+
}

src/Models/Menu.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Pharaonic\Laravel\Menus\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Pharaonic\Laravel\Translatable\Translatable;
7+
8+
/**
9+
* @property integer $id
10+
* @property string $section
11+
* @property string $url
12+
* @property integer $sort
13+
* @property integer $visible
14+
* @property Carbon $created_at
15+
* @property Carbon $updated_at
16+
* @property MenuTranslation $translations
17+
*
18+
* @author Moamen Eltouny (Raggi) <raggi@raggitech.com>
19+
*/
20+
class Menu extends Model
21+
{
22+
use Translatable;
23+
24+
/**
25+
* Fields List
26+
*
27+
* @var array
28+
*/
29+
protected $fillable = ['section', 'url', 'sort', 'visible'];
30+
31+
/**
32+
* Translatable attributes names.
33+
*
34+
* @var array
35+
*/
36+
protected $translatableAttributes = ['title'];
37+
38+
/**
39+
* The attributes that should be cast.
40+
*
41+
* @var array
42+
*/
43+
protected $casts = [
44+
'data' => 'array',
45+
'sort' => 'integer',
46+
'visible' => 'boolean',
47+
];
48+
49+
/**
50+
* Get section'items.
51+
*
52+
* @param \Illuminate\Database\Eloquent\Builder $query
53+
* @param string $section
54+
* @param string|null $locale
55+
* @return \Illuminate\Database\Eloquent\Builder
56+
*/
57+
public function scopeSection($query, string $section, string $locale = null)
58+
{
59+
return $query->translated($locale)->where('section', $section)->where('visible', true)->orderBy('sort', 'ASC');
60+
}
61+
62+
/**
63+
* Create a localized menu.
64+
*
65+
* @param string $section
66+
* @param string $title
67+
* @param string $url
68+
* @param integer $sort
69+
* @param boolean $visible
70+
* @param string $locale
71+
* @return Menu
72+
*/
73+
public static function create(string $section, string $title, string $url, int $sort = 0, bool $visible = true, string $locale = null)
74+
{
75+
$menu = new self;
76+
$menu->section = $section;
77+
$menu->url = $url;
78+
$menu->sort = $sort;
79+
$menu->visible = $visible;
80+
$menu->save();
81+
82+
$menu->translateOrNew($locale ?? app()->getLocale())->title = $title;
83+
$menu->save();
84+
85+
return $menu;
86+
}
87+
}

src/Models/MenuTranslation.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Pharaonic\Laravel\Menus\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* @property integer $id
9+
* @property integer $menu_id
10+
* @property string $locale
11+
* @property string $title
12+
*
13+
* @author Moamen Eltouny (Raggi) <raggi@raggitech.com>
14+
*/
15+
class MenuTranslation extends Model
16+
{
17+
/**
18+
* The attributes that are mass assignable.
19+
*
20+
* @var array
21+
*/
22+
protected $fillable = ['locale', 'menu_id', 'title'];
23+
24+
/**
25+
* Indicates if the model should be timestamped.
26+
*
27+
* @var bool
28+
*/
29+
public $timestamps = false;
30+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateMenusTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('menus', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
19+
$table->string('section', 40);
20+
$table->string('url');
21+
22+
$table->integer('sort')->default(0);
23+
$table->boolean('visible')->default(true);
24+
25+
$table->timestamps();
26+
});
27+
}
28+
29+
/**
30+
* Reverse the migrations.
31+
*
32+
* @return void
33+
*/
34+
public function down()
35+
{
36+
Schema::dropIfExists('menus');
37+
}
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateMenuTranslationsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('menu_translations', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->string('title');
19+
20+
$table->string('locale')->index();
21+
$table->unsignedBigInteger('menu_id');
22+
$table->unique(['menu_id', 'locale']);
23+
$table->foreign('menu_id')->references('id')->on('menus')->onDelete('cascade');
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('menu_translations');
35+
}
36+
}

src/views/section.blade.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<ul>
2+
@foreach($section as $item)
3+
<li><a href="{{ $item->url }}">{!! $item->title !!}</a></li>
4+
@endforeach
5+
</ul>

0 commit comments

Comments
 (0)