Skip to content

Commit 802d20e

Browse files
committed
Fixed generation slug without trashed
1 parent fc0fe9a commit 802d20e

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

app/Models/Post.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#[ScopedBy([PublishedScope::class])]
3535
class Post extends Model
3636
{
37-
use AsSource, Chartable, Filterable, HasAuthor, HasFactory, Likeable, LogsActivityFillable, Searchable, SoftDeletes, Taggable;
37+
use AsSource, Chartable, Filterable, HasAuthor, HasFactory, Likeable, LogsActivityFillable, Searchable, Taggable, SoftDeletes;
3838

3939
/**
4040
* @var string[]
@@ -53,11 +53,11 @@ class Post extends Model
5353
* @var array
5454
*/
5555
protected $casts = [
56-
'title' => 'string',
57-
'content' => 'string',
58-
'slug' => 'string',
59-
'type' => PostTypeEnum::class,
60-
'status' => StatusEnum::class,
56+
'title' => 'string',
57+
'content' => 'string',
58+
'slug' => 'string',
59+
'type' => PostTypeEnum::class,
60+
'status' => StatusEnum::class,
6161
'published_at' => 'datetime',
6262
];
6363

@@ -86,22 +86,23 @@ public static function boot()
8686

8787
static::creating(function ($post) {
8888

89-
if ($post->published_at === null) {
89+
if($post->published_at === null) {
9090
$post->published_at = now();
9191
}
9292

9393
$slug = Str::slug($post->title);
9494
$i = 1;
9595

96-
while (static::where('slug', $slug)->withTrashed()->exists()) {
97-
$slug = Str::slug($post->title).'-'.$i++;
96+
while (static::withoutGlobalScopes()->where('slug', $slug)->withTrashed()->exists()) {
97+
$slug = Str::slug($post->title) . '-' . $i++;
9898
}
9999

100+
100101
$post->slug = $slug;
101102
});
102103

103104
static::created(function (Post $post) {
104-
dispatch(fn () => $this->notifyAboutPublishedPost($post))->afterResponse();
105+
dispatch(fn() => $this->notifyAboutPublishedPost($post))->afterResponse();
105106
});
106107
}
107108

@@ -202,7 +203,6 @@ public function getDescriptionAttribute()
202203

203204
/**
204205
* @param Post $post
205-
*
206206
* @return void
207207
*/
208208
public function notifyAboutPublishedPost(Post $post): void

0 commit comments

Comments
 (0)