34
34
#[ScopedBy([PublishedScope::class])]
35
35
class Post extends Model
36
36
{
37
- use AsSource, Chartable, Filterable, HasAuthor, HasFactory, Likeable, LogsActivityFillable, Searchable, SoftDeletes, Taggable ;
37
+ use AsSource, Chartable, Filterable, HasAuthor, HasFactory, Likeable, LogsActivityFillable, Searchable, Taggable, SoftDeletes ;
38
38
39
39
/**
40
40
* @var string[]
@@ -53,11 +53,11 @@ class Post extends Model
53
53
* @var array
54
54
*/
55
55
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,
61
61
'published_at ' => 'datetime ' ,
62
62
];
63
63
@@ -86,22 +86,23 @@ public static function boot()
86
86
87
87
static ::creating (function ($ post ) {
88
88
89
- if ($ post ->published_at === null ) {
89
+ if ($ post ->published_at === null ) {
90
90
$ post ->published_at = now ();
91
91
}
92
92
93
93
$ slug = Str::slug ($ post ->title );
94
94
$ i = 1 ;
95
95
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 ++;
98
98
}
99
99
100
+
100
101
$ post ->slug = $ slug ;
101
102
});
102
103
103
104
static ::created (function (Post $ post ) {
104
- dispatch (fn () => $ this ->notifyAboutPublishedPost ($ post ))->afterResponse ();
105
+ dispatch (fn () => $ this ->notifyAboutPublishedPost ($ post ))->afterResponse ();
105
106
});
106
107
}
107
108
@@ -202,7 +203,6 @@ public function getDescriptionAttribute()
202
203
203
204
/**
204
205
* @param Post $post
205
- *
206
206
* @return void
207
207
*/
208
208
public function notifyAboutPublishedPost (Post $ post ): void
0 commit comments