3
3
namespace Vlados \LaravelUniqueUrls ;
4
4
5
5
use Exception ;
6
- use Illuminate \Database \Eloquent \Casts \Attribute ;
7
6
use Illuminate \Database \Eloquent \Model ;
8
7
use Illuminate \Database \Eloquent \Relations \MorphOne ;
9
8
use Illuminate \Support \Str ;
@@ -64,57 +63,43 @@ public function url(): MorphOne
64
63
return $ this ->morphOne (Url::class, 'related ' );
65
64
}
66
65
67
- /**
68
- * Returns the absolute url for the model.
69
- *
70
- * @return string
71
- *
72
- * @throws Throwable
73
- */
74
- private function getUrl ($ absolute = true ): string
75
- {
76
- $ url = $ this ->url ()->where ('language ' , app ()->getLocale ())->first ()->slug ?? '' ;
77
-
78
- return $ absolute ? url ($ url ) : $ url ;
79
- }
80
-
81
66
public function urlStrategy (): string
82
67
{
83
68
return Str::slug ($ this ->getAttribute ('name ' ));
84
69
}
85
70
86
- /**
87
- * Generate automatically the urls on create. You can disable it and manually trigger it after.
88
- *
89
- * @return bool
90
- */
91
71
public function isAutoGenerateUrls (): bool
92
72
{
93
73
return $ this ->autoGenerateUrls ;
94
74
}
95
75
96
- /**
97
- * @param bool $autoGenerateUrls
98
- */
99
76
public function setAutoGenerateUrls (bool $ autoGenerateUrls ): void
100
77
{
101
78
$ this ->autoGenerateUrls = $ autoGenerateUrls ;
102
79
}
103
80
81
+ public function getRelativeUrlAttribute (): string
82
+ {
83
+ return $ this ->getUrl (false );
84
+ }
85
+
86
+ public function getAbsoluteUrlAttribute (): string
87
+ {
88
+ return $ this ->getUrl (true );
89
+ }
90
+
104
91
protected static function bootHasUniqueUrlTrait (): void
105
92
{
106
93
static ::created (function (Model $ model ) {
107
- if ($ model ->isAutoGenerateUrls () === false ) {
108
- return ;
94
+ if ($ model ->isAutoGenerateUrls () === true ) {
95
+ $ model -> generateUrl () ;
109
96
}
110
- $ model ->generateUrl ();
111
97
});
112
98
113
99
static ::updated (function (Model $ model ) {
114
- if ($ model ->isAutoGenerateUrls () === false ) {
115
- return ;
100
+ if ($ model ->isAutoGenerateUrls () === true ) {
101
+ $ model -> generateUrlOnUpdate () ;
116
102
}
117
- $ model ->generateUrlOnUpdate ();
118
103
});
119
104
}
120
105
@@ -123,29 +108,26 @@ protected function generateUrlOnUpdate(): void
123
108
$ unique_url = Url::makeSlug ($ this ->urlStrategy (), $ this );
124
109
$ this ->url ()->get ()->each (function (Url $ url ) use ($ unique_url ) {
125
110
$ prefix = config ('app.fallback_locale ' ) === $ url ->getAttribute ('language ' ) ? '' : $ url ->getAttribute ('language ' ) . '/ ' ;
126
- if ($ url ->getAttribute ('slug ' ) === $ prefix . $ unique_url ) {
127
- return ;
111
+ if ($ url ->getAttribute ('slug ' ) !== $ prefix . $ unique_url ) {
112
+ $ url ->update ([
113
+ 'slug ' => $ prefix . $ unique_url ,
114
+ ]);
115
+ $ url ->save ();
128
116
}
129
-
130
- $ url ->update ([
131
- 'slug ' => $ prefix . $ unique_url ,
132
- ]);
133
- $ url ->save ();
134
117
});
135
118
}
136
119
137
120
/**
138
- * Interact with the user's address.
121
+ * Returns the absolute url for the model.
122
+ *
123
+ * @return string
139
124
*
140
- * @return Attribute
125
+ * @throws Throwable
141
126
*/
142
- public function getRelativeUrlAttribute ( ): string
127
+ private function getUrl ( $ absolute = true ): string
143
128
{
144
- return $ this ->getUrl (false );
145
- }
129
+ $ url = $ this ->url ()->where ('language ' , app ()->getLocale ())->first ()->slug ?? '' ;
146
130
147
- public function getAbsoluteUrlAttribute (): string
148
- {
149
- return $ this ->getUrl (true );
131
+ return $ absolute ? url ($ url ) : $ url ;
150
132
}
151
133
}
0 commit comments