@@ -16,74 +16,54 @@ public static function bootHasSlug()
16
16
{
17
17
static ::saving (function ($ model ) {
18
18
$ attribute = $ model ->getSlugStorageAttribute ();
19
-
20
- if (!is_null ($ model ->attributes [$ attribute ])) return ;
21
-
22
- $ model ->attributes [$ attribute ] = $ model ->generateSlug ();
19
+
20
+ if ($ model ->attributeIsTranslatable ($ attribute )) {
21
+ $ model ->translateSlugs ($ attribute );
22
+ } else {
23
+ if (!is_null ($ model ->attributes [$ attribute ])) return ;
24
+
25
+ $ model ->attributes [$ attribute ] = str_slug ($ model ->$ sluggable );
26
+ }
23
27
});
24
28
}
25
29
26
30
/**
27
- * Get the attribute name used to generate the slug from
31
+ * Generate translated slugs. Keeps any existing translated slugs.
28
32
*
29
- * @return string
33
+ * @param string $attribute
30
34
*/
31
- public function getSluggable ( )
35
+ public function translateSlugs ( $ attribute )
32
36
{
33
- return $ this ->sluggable ;
34
- }
37
+ $ sluggable = $ this ->getSluggable () ;
38
+ $ value = json_decode ( $ this -> attributes [ $ attribute ], true );
35
39
36
- /**
37
- * Get the attribute name used to store the slug into
38
- *
39
- * @return string
40
- */
41
- public function getSlugStorageAttribute ()
42
- {
43
- return $ this ->slugStorageAttribute ?? 'slug ' ;
44
- }
40
+ foreach ($ this ->getTranslatedLocales ($ this ->getSluggable ()) as $ locale ) {
41
+ if (!isset ($ value [$ locale ]) || is_null ($ value [$ locale ])) {
42
+ $ value [$ locale ] = str_slug ($ this ->getTranslation ($ sluggable , $ locale ));
43
+ }
44
+ }
45
45
46
- /**
47
- * Generate the slug.
48
- *
49
- * @return false|string
50
- */
51
- protected function generateSlug ()
52
- {
53
- return $ this ->slugify ($ this ->getSluggable ());
46
+ $ this ->attributes [$ attribute ] = json_encode ($ value );
54
47
}
55
48
56
49
/**
57
- * Handle the slug generation for translatable and
58
- * non-translatable attributes.
50
+ * Get the attribute name used to generate the slug from
59
51
*
60
- * @param string $attribute
61
- * @return false|string
52
+ * @return string
62
53
*/
63
- protected function slugify ( $ attribute )
54
+ public function getSluggable ( )
64
55
{
65
- if ($ this ->attributeIsTranslatable ($ attribute )) {
66
- return json_encode ($ this ->translatedSlugs ($ attribute ));
67
- }
68
-
69
- return str_slug ($ this ->$ attribute );
56
+ return $ this ->sluggable ;
70
57
}
71
58
72
59
/**
73
- * Handle the generation of translated slugs
60
+ * Get the attribute name used to store the slug into
74
61
*
75
- * @param string $attribute
76
- * @return array
62
+ * @return string
77
63
*/
78
- protected function translatedSlugs ( $ attribute )
64
+ public function getSlugStorageAttribute ( )
79
65
{
80
- $ slugs = [];
81
-
82
- foreach ($ this ->getTranslatedLocales ($ attribute ) as $ locale ) {
83
- $ slugs [$ locale ] = str_slug ($ this ->getTranslation ($ attribute , $ locale ));
84
- }
85
-
86
- return $ slugs ;
66
+ return $ this ->slugStorageAttribute ?? 'slug ' ;
87
67
}
88
68
89
69
/**
@@ -135,7 +115,7 @@ public function getSluggedUrlForRoute(Route $route, $locale = null, $fullUrl = t
135
115
}
136
116
137
117
/**
138
- * Get a bound route's parameters with the
118
+ * Get a bound route's parameters with the
139
119
* model's slug set to the desired locale.
140
120
*
141
121
* @param Illuminate\Routing\Route $route
0 commit comments