@@ -64,8 +64,12 @@ public function getUniqueSlug($sluggable, $locale = null)
64
64
65
65
$ slug = str_slug ($ sluggable );
66
66
67
- $ i = 1 ;
68
- while ($ this ->slugExists ($ slug , $ locale )) {
67
+ $ i = 0 ;
68
+ while ($ this ->slugExists ($ slug , $ locale , $ i )) {
69
+ $ i ++;
70
+ }
71
+
72
+ if ($ i ) {
69
73
$ slug = $ slug . '- ' . $ i ;
70
74
}
71
75
@@ -77,14 +81,18 @@ public function getUniqueSlug($sluggable, $locale = null)
77
81
*
78
82
* @param string $slug
79
83
* @param string|null $locale
84
+ * @param int $i
80
85
* @return bool
81
86
*/
82
- public function slugExists ($ slug , $ locale = null )
87
+ public function slugExists ($ slug , $ locale = null , $ i = 0 )
83
88
{
84
89
$ whereKey = is_null ($ locale ) ? $ this ->getSlugStorageAttribute () : $ this ->getSlugStorageAttribute ().'-> ' .$ locale ;
85
90
86
- $ query = static ::where ($ whereKey , $ slug )
87
- ->withoutGlobalScopes ();
91
+ if ($ i ) {
92
+ $ slug = $ slug . '- ' . $ i ;
93
+ }
94
+
95
+ $ query = $ this ->getSlugExistsQuery ($ whereKey , $ slug );
88
96
89
97
if ($ this ->usesSoftDeletes ()) {
90
98
$ query ->withTrashed ();
@@ -93,6 +101,19 @@ public function slugExists($slug, $locale = null)
93
101
return $ query ->exists ();
94
102
}
95
103
104
+ /**
105
+ * Get the query that checks if the slug already exists
106
+ *
107
+ * @param string $whereKey
108
+ * @param string $slug
109
+ * @return \Illuminate\Database\Eloquent\Builder
110
+ */
111
+ protected function getSlugExistsQuery ($ whereKey , $ slug )
112
+ {
113
+ return $ query = static ::where ($ whereKey , $ slug )
114
+ ->withoutGlobalScopes ();
115
+ }
116
+
96
117
/**
97
118
* Check if model uses soft deletes
98
119
*
0 commit comments