@@ -7,28 +7,26 @@ trait HasTitleMenuLabelTrait
7
7
/**
8
8
* Get the display label for menu items.
9
9
* This implementation uses the 'title' field with locale support.
10
+ * Assumes the model uses Spatie Laravel Translatable trait.
10
11
*/
11
12
public function getMenuLabel (?string $ locale = null ): string
12
13
{
13
14
$ locale = $ locale ?: app ()->getLocale ();
14
15
15
- // Check if the model uses translations (like Spatie Laravel Translatable)
16
- if (method_exists ($ this , 'getTranslation ' )) {
17
- $ title = $ this ->getTranslation ('title ' , $ locale );
18
- if (empty ($ title )) {
19
- // Fallback to the configured fallback locale
20
- $ title = $ this ->getTranslation ('title ' , config ('app.fallback_locale ' , 'en ' ));
21
- }
22
-
23
- return $ title ?: 'Untitled ' ;
16
+ // Get translated title (assumes HasTranslations trait is used)
17
+ $ title = $ this ->getTranslation ('title ' , $ locale );
18
+ if (empty ($ title )) {
19
+ // Fallback to the configured fallback locale
20
+ $ title = $ this ->getTranslation ('title ' , config ('app.fallback_locale ' , 'en ' ));
24
21
}
25
22
26
- return $ this -> title ?: 'Untitled ' ;
23
+ return $ title ?: 'Untitled ' ;
27
24
}
28
25
29
26
/**
30
27
* Scope to search for models that can be used in menu items.
31
28
* This implementation searches in common searchable fields.
29
+ * Assumes the model uses Spatie Laravel Translatable trait.
32
30
*/
33
31
public function scopeSearchForMenuItems ($ query , string $ search )
34
32
{
@@ -39,8 +37,7 @@ public function scopeSearchForMenuItems($query, string $search)
39
37
$ searchableFields = ['name ' , 'intro ' , 'description ' , 'overview_title ' ];
40
38
foreach ($ searchableFields as $ field ) {
41
39
if (in_array ($ field , $ this ->getFillable ()) ||
42
- (method_exists ($ this , 'getTranslatableAttributes ' ) &&
43
- in_array ($ field , $ this ->getTranslatableAttributes ()))) {
40
+ in_array ($ field , $ this ->getTranslatableAttributes ())) {
44
41
$ query ->orWhere ($ field , 'like ' , "% {$ search }% " );
45
42
}
46
43
}
0 commit comments