1111 * @property string $url
1212 * @property integer $sort
1313 * @property integer $visible
14+ * @property integer $parent_id
1415 * @property Carbon $created_at
1516 * @property Carbon $updated_at
17+ * @property Menu $children
1618 * @property MenuTranslation $translations
1719 *
1820 * @author Moamen Eltouny (Raggi) <raggi@raggitech.com>
@@ -26,7 +28,7 @@ class Menu extends Model
2628 *
2729 * @var array
2830 */
29- protected $ fillable = ['section ' , 'url ' , 'sort ' , 'visible ' ];
31+ protected $ fillable = ['section ' , 'url ' , 'sort ' , 'visible ' , ' parent_id ' ];
3032
3133 /**
3234 * Translatable attributes names.
@@ -47,7 +49,7 @@ class Menu extends Model
4749 ];
4850
4951 /**
50- * Get section' items.
52+ * Get section items.
5153 *
5254 * @param \Illuminate\Database\Eloquent\Builder $query
5355 * @param string $section
@@ -56,7 +58,34 @@ class Menu extends Model
5658 */
5759 public function scopeSection ($ query , string $ section , string $ locale = null )
5860 {
59- return $ query ->translated ($ locale )->where ('section ' , $ section )->where ('visible ' , true )->orderBy ('sort ' , 'ASC ' );
61+ return $ query ->translated ($ locale )->with ([
62+ 'children ' => function ($ q ) {
63+ return $ q ->visible ();
64+ },
65+ 'children.translations '
66+ ])->where ([
67+ 'section ' => $ section ,
68+ 'parent_id ' => null
69+ ])->visible ()->orderBy ('sort ' , 'ASC ' );
70+ }
71+
72+ /**
73+ * Get visible items.
74+ *
75+ * @param \Illuminate\Database\Eloquent\Builder $query
76+ * @return \Illuminate\Database\Eloquent\Builder
77+ */
78+ public function scopeVisible ($ query )
79+ {
80+ return $ query ->where ('visible ' , true );
81+ }
82+
83+ /**
84+ * @return \Illuminate\Database\Eloquent\Relations\HasMany
85+ */
86+ public function children ()
87+ {
88+ return $ this ->hasMany (Menu::class, 'parent_id ' , 'id ' );
6089 }
6190
6291 /**
@@ -70,18 +99,25 @@ public function scopeSection($query, string $section, string $locale = null)
7099 * @param string $locale
71100 * @return Menu
72101 */
73- public static function set (string $ section , string $ title , string $ url , int $ sort = 0 , bool $ visible = true , string $ locale = null )
102+ public static function set (string $ section , mixed $ title , string $ url , int $ parent = null , int $ sort = 0 , bool $ visible = true )
74103 {
75- $ menu = new self ;
76- $ menu ->section = $ section ;
77- $ menu ->url = $ url ;
78- $ menu ->sort = $ sort ;
79- $ menu ->visible = $ visible ;
80- $ menu ->save ();
104+ $ menu = new self ;
105+ $ data = [
106+ 'section ' => $ section ,
107+ 'url ' => $ url ,
108+ 'parent_id ' => $ parent ,
109+ 'sort ' => $ sort ,
110+ 'visible ' => $ visible
111+ ];
112+
113+ $ localKey = $ menu ->translationsKey ?? 'locale ' ;
81114
82- $ menu ->translateOrNew ($ locale ?? app ()->getLocale ())->title = $ title ;
83- $ menu ->save ();
115+ if (is_array ($ title ))
116+ $ data [$ localKey ] = $ title ;
117+ else
118+ $ data [$ localKey ][app ()->getLocale ()]['title ' ] = $ title ;
84119
120+ $ menu ->fill ($ data )->save ();
85121 return $ menu ;
86122 }
87123}
0 commit comments