diff --git a/app/Livewire/CreatePost.php b/app/Livewire/CreatePost.php index 4fb3e4e..1fe2ad3 100644 --- a/app/Livewire/CreatePost.php +++ b/app/Livewire/CreatePost.php @@ -2,37 +2,27 @@ namespace App\Livewire; -use App\Models\Post; -use Livewire\Attributes\Validate; use Livewire\Component; +use App\Livewire\Forms\PostForm; class CreatePost extends Component { - #[Validate('required|min:5')] - public string $title = ''; - - #[Validate('required|min:5')] - public string $body = ''; + public PostForm $form; public bool $success = false; - public function render() - { - return view('livewire.create-post'); - } - public function save(): void { $this->validate(); - Post::create([ - 'title' => $this->title, - 'body' => $this->body, - ]); + $this->form->save(); $this->success = true; + } - $this->reset('title', 'body'); + public function render() + { + return view('livewire.create-post'); } } diff --git a/app/Livewire/EditPost.php b/app/Livewire/EditPost.php new file mode 100644 index 0000000..bdb738c --- /dev/null +++ b/app/Livewire/EditPost.php @@ -0,0 +1,33 @@ +form->setPost($post); + } + + public function update(): void + { + $this->validate(); + + $this->form->update(); + + $this->success = true; + } + + public function render() + { + return view('livewire.edit-post'); + } +} \ No newline at end of file diff --git a/app/Livewire/Forms/PostForm.php b/app/Livewire/Forms/PostForm.php new file mode 100644 index 0000000..f49abdb --- /dev/null +++ b/app/Livewire/Forms/PostForm.php @@ -0,0 +1,39 @@ +post = $post; + + $this->title = $post->title; + + $this->body = $post->body; + } + + public function save(): void + { + Post::create($this->all()); + + $this->reset('title', 'body'); + } + + public function update(): void + { + $this->post->update($this->all()); + } +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 7f93bd8..a94aa16 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "project2", + "name": "project", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/resources/views/livewire/create-post.blade.php b/resources/views/livewire/create-post.blade.php index 2749386..e9d3c54 100644 --- a/resources/views/livewire/create-post.blade.php +++ b/resources/views/livewire/create-post.blade.php @@ -3,19 +3,19 @@ Post saved successfully. @endif -
+
- - @error('title') + + @error('form.title') {{ $message }} @enderror
- - @error('body') + + @error('form.body') {{ $message }} @enderror
diff --git a/resources/views/livewire/edit-post.blade.php b/resources/views/livewire/edit-post.blade.php new file mode 100644 index 0000000..2f23614 --- /dev/null +++ b/resources/views/livewire/edit-post.blade.php @@ -0,0 +1,27 @@ +
+ @if($success) + Post saved successfully. + @endif + + +
+ + + @error('form.title') + {{ $message }} + @enderror +
+ +
+ + + @error('form.body') + {{ $message }} + @enderror +
+ + + +
diff --git a/resources/views/posts/edit.blade.php b/resources/views/posts/edit.blade.php new file mode 100644 index 0000000..f50c683 --- /dev/null +++ b/resources/views/posts/edit.blade.php @@ -0,0 +1,17 @@ + + +

+ {{ __('Edit Post') }} +

+
+ +
+
+
+
+ +
+
+
+
+
diff --git a/routes/web.php b/routes/web.php index a4dc4cd..d873e4a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -29,4 +29,5 @@ require __DIR__.'/auth.php'; -Route::view('posts/create', 'posts.create'); \ No newline at end of file +Route::view('posts/create', 'posts.create'); +Route::view('posts/{post}/edit', 'posts.edit'); \ No newline at end of file