From 517c2a71f7cee2124a2e61c74ac750971d8eac6d Mon Sep 17 00:00:00 2001 From: raccoon254 Date: Sun, 16 Jun 2024 08:40:58 +0300 Subject: [PATCH] Correctly show messages for the user if already added to mailing list --- app/Livewire/Newsletter.php | 9 +++++- resources/views/livewire/newsletter.blade.php | 31 ++++++++++++------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/app/Livewire/Newsletter.php b/app/Livewire/Newsletter.php index a75dc84..df01cee 100644 --- a/app/Livewire/Newsletter.php +++ b/app/Livewire/Newsletter.php @@ -11,13 +11,20 @@ class Newsletter extends Component public $email; protected $rules = [ - 'email' => 'required|email|unique:subscribers,email', + 'email' => 'required|email', ]; public function subscribe(): void { $this->validate(); + //if the email already exists in the database, return an error message + if (Subscriber::where('email', $this->email)->exists()) { + session()->flash('message', 'You have already subscribed to the newsletter!'); + + return; + } + Subscriber::create(['email' => $this->email]); session()->flash('message', 'You have successfully subscribed to the newsletter!'); diff --git a/resources/views/livewire/newsletter.blade.php b/resources/views/livewire/newsletter.blade.php index a8795ed..9673368 100644 --- a/resources/views/livewire/newsletter.blade.php +++ b/resources/views/livewire/newsletter.blade.php @@ -1,13 +1,20 @@ -
-
- @csrf - - @error('email') -

{{ $message }}

- @enderror - - +
+ @if (session()->has('message')) +
+ {{ session('message') }} +
+ @endif +
+
+ @csrf + + @error('email') +

{{ $message }}

+ @enderror + + +