data = $this->populate($id); } #[On('post.create')] public function handlePostCreate(): void { $this->data = $this->populate(0); } #[On('post.edit')] public function handlePostEdit($id): void { $this->data = $this->populate($id); } public function process(): void { $this->data->slug = Str::kebab($this->data->title); $this->validate(); if ($this->data->id) { $post = Post::findOrFail($this->data->id); $post->update($this->data->toArray()); if (count($this->data->tags) > 0) { $post->tags()->detach(); $post->tags()->attach($this->data->tags); } Flux::toast(text: __('Post updated.'), variant: 'success'); } else { $post = Post::create($this->data->toArray()); if (count($this->data->tags) > 0) { $post->tags()->attach($this->data->tags); } Flux::toast(text: __('Post created.'), variant: 'success'); } $this->data = $this->populate(0); $this->dispatch('post.processed'); } public function populate($id): PostData { if ($id) { $post = PostData::from($model = Post::findOrFail($id)); $post->tags = $model->tags->pluck('id')->toArray(); return $post; } return PostData::blank(); } public function rules(): array { $rules = PostData::rules(); return Arr::prependKeysWith( array: $rules, prependWith: 'data.' ); } public function messages(): array { return Arr::prependKeysWith( array: PostData::messages(), prependWith: 'data.' ); } public function updatedDataSlug(): void { $this->data->slug = Str::kebab($this->data->title); } #[Computed] public function authors(): Collection { return User::orderBy('name')->get(); } #[Computed] public function categories(): Collection { return Category::orderBy('name')->get(); } #[Computed] public function tags(): Collection { return Tag::orderBy('name')->get(); } }; ?>