diff --git a/app/Livewire/BlogMaker.php b/app/Livewire/BlogMaker.php index caac560..ffcca89 100644 --- a/app/Livewire/BlogMaker.php +++ b/app/Livewire/BlogMaker.php @@ -17,13 +17,32 @@ class BlogMaker extends Component public $content = ''; public $contentHtml; - public function saveBlog(): RedirectResponse + public function mount(): void + { + //check if session has blog data + if (session()->has('blog')) { + $blog = session()->get('blog'); + $this->title = $blog['title']; + $this->content = $blog['content']; + //alert + session()->flash('message', 'Blog data restored from session'); + } + + } + + public function saveBlog(): mixed { $this->validate([ 'title' => 'required|min:6', 'content' => 'required|min:6', ]); + //save blog to session + session()->put('blog', [ + 'title' => $this->title, + 'content' => $this->content, + ]); + $blogData = [ 'title' => $this->title, 'content' => $this->content, // Storing HTML content directly @@ -31,7 +50,14 @@ public function saveBlog(): RedirectResponse 'user_id' => Auth::id(), ]; - $blog = Blog::create($blogData); + try { + $blog = Blog::create($blogData); + } catch (\Exception $e) { + if (Str::contains($e->getMessage(), 'blogs_slug_unique')) { + return redirect()->route('blog.create')->with('error', 'Blog with this title already exists'); + } + return redirect()->route('blog.create' )->with('error', 'Failed to create blog' . $e->getMessage()); + } return redirect()->route('blogs.show', $blog->slug); } diff --git a/resources/views/blogs/create.blade.php b/resources/views/blogs/create.blade.php index 15bf2a0..c8d4e7a 100644 --- a/resources/views/blogs/create.blade.php +++ b/resources/views/blogs/create.blade.php @@ -1,10 +1,10 @@ -