From c3b814667cea12cf3c2b1703e7b358e97390f501 Mon Sep 17 00:00:00 2001 From: hafidhmuhammadakbar Date: Sun, 11 Jun 2023 16:22:04 +0700 Subject: [PATCH] add notifications in medicine and accounts --- app/Http/Controllers/MedicineController.php | 18 +++++++++++++----- app/Http/Controllers/UserController.php | 17 +++++++++++++---- resources/views/accounts/create.blade.php | 10 ++++++++++ resources/views/accounts/edit.blade.php | 10 ++++++++++ resources/views/accounts/index.blade.php | 6 ++++++ resources/views/medicines/create.blade.php | 10 ++++++++++ resources/views/medicines/edit.blade.php | 10 ++++++++++ resources/views/medicines/index.blade.php | 6 ++++++ 8 files changed, 78 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/MedicineController.php b/app/Http/Controllers/MedicineController.php index f31874c..02cbd38 100644 --- a/app/Http/Controllers/MedicineController.php +++ b/app/Http/Controllers/MedicineController.php @@ -40,9 +40,13 @@ public function store(Request $request) $validatedData['discount'] = $validatedData['discount'] / 100; - Medicine::create($validatedData); + $success = Medicine::create($validatedData); - return redirect()->route('medicines.index')->with('success', 'Obat berhasil ditambahkan'); + if ($success) { + return redirect()->route('medicines.index')->with('success', 'Medicine add successfully'); + } else { + return redirect()->route('medicines.create')->withErrors('Medicine failed to add'); + } } public function create() @@ -68,9 +72,13 @@ public function update(Request $request, Medicine $medicine) $validatedData['discount'] = $validatedData['discount'] / 100; - $medicine->update($validatedData); - - return redirect()->route('medicines.index')->with('success', 'Medicine updated successfully'); + $success = $medicine->update($validatedData); + + if ($success) { + return redirect()->route('medicines.index')->with('success', 'Medicine updated successfully'); + } else { + return redirect()->route('medicines.edit')->withErrors('Medicine failed to add'); + } } public function destroy(Medicine $medicine) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 6ae4b4d..d48c8a1 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -40,9 +40,14 @@ public function store(Request $request) 'joining_date' => 'required|date', ]); - User::create($request->all()); + $success = User::create($request->all()); - return redirect()->route('accounts.index')->with('success', 'User created successfully'); + if (!$success) { + return redirect()->route('accounts.index')->with('success', 'User add successfully'); + } + else{ + return redirect()->route('accounts.create')->withErrors('User failed to add'); + } } public function edit(User $user) @@ -59,9 +64,13 @@ public function update(Request $request, User $user) 'role' => 'required', ]); - $user->update($request->all()); + $success = $user->update($request->all()); - return redirect()->route('accounts.index')->with('success', 'User updated successfully'); + if ($success) { + return redirect()->route('accounts.index')->with('success', 'User updated successfully'); + } else { + return redirect()->route('accounts.edit', $user->id)->withErrors('User failed to update'); + } } public function destroy(User $user) diff --git a/resources/views/accounts/create.blade.php b/resources/views/accounts/create.blade.php index 2f8d53c..91ca912 100644 --- a/resources/views/accounts/create.blade.php +++ b/resources/views/accounts/create.blade.php @@ -2,6 +2,16 @@ @section('content')
+ {{-- if error to add accounts --}} + @if ($errors->any()) +
+
    + @foreach ($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+ @endif
diff --git a/resources/views/accounts/edit.blade.php b/resources/views/accounts/edit.blade.php index 1829b1e..98e1a0e 100644 --- a/resources/views/accounts/edit.blade.php +++ b/resources/views/accounts/edit.blade.php @@ -2,6 +2,16 @@ @section('content')
+ {{-- if error to edit accounts --}} + @if ($errors->any()) +
+
    + @foreach ($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+ @endif
diff --git a/resources/views/accounts/index.blade.php b/resources/views/accounts/index.blade.php index c111206..d1c21ed 100644 --- a/resources/views/accounts/index.blade.php +++ b/resources/views/accounts/index.blade.php @@ -2,6 +2,12 @@ @section('content')
+ {{-- if add or edit success --}} + @if(session('success')) +
+ {{ session('success') }} +
+ @endif
diff --git a/resources/views/medicines/create.blade.php b/resources/views/medicines/create.blade.php index ba20389..53e4b43 100644 --- a/resources/views/medicines/create.blade.php +++ b/resources/views/medicines/create.blade.php @@ -2,6 +2,16 @@ @section('content')
+ {{-- if error to add medicine --}} + @if ($errors->any()) +
+
    + @foreach ($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+ @endif
diff --git a/resources/views/medicines/edit.blade.php b/resources/views/medicines/edit.blade.php index 540e700..a3a382c 100644 --- a/resources/views/medicines/edit.blade.php +++ b/resources/views/medicines/edit.blade.php @@ -2,6 +2,16 @@ @section('content')
+ {{-- if error to edit medicines --}} + @if ($errors->any()) +
+
    + @foreach ($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+ @endif
diff --git a/resources/views/medicines/index.blade.php b/resources/views/medicines/index.blade.php index 210176b..07141c9 100644 --- a/resources/views/medicines/index.blade.php +++ b/resources/views/medicines/index.blade.php @@ -2,6 +2,12 @@ @section('content')
+ {{-- if add or edit success --}} + @if(session('success')) +
+ {{ session('success') }} +
+ @endif