From 034000bcefa3e38cb4ee46f81bc4ab88fb23aa3a Mon Sep 17 00:00:00 2001 From: Moradi Date: Wed, 12 Mar 2025 16:42:00 +0330 Subject: [PATCH] fix the issue with the previous commit. event was called before the labels attached to a cycle. --- app/Http/Controllers/CycleController.php | 4 ++++ app/Listeners/UpdateLabelsCycleNumber.php | 1 + app/Models/Cycle.php | 10 ---------- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/CycleController.php b/app/Http/Controllers/CycleController.php index b7807977..b9451522 100644 --- a/app/Http/Controllers/CycleController.php +++ b/app/Http/Controllers/CycleController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Events\CycleSaved; use App\Http\Requests\StoreCycleRequest; use App\Http\Resources\CycleResource; use App\Interfaces\CycleRepositoryInterface; @@ -72,6 +73,8 @@ class CycleController extends Controller foreach ( $Labels as $Label => $Count ) { $CycleObj->labels()->attach( array( $Label ) , [ 'count' => $Count ] ); } + event(new CycleSaved($CycleObj)); + return MessageResponse::success( trans( 'messages.success' ) ); } @@ -109,6 +112,7 @@ class CycleController extends Controller foreach ( $Labels as $Label => $Count ) { $Cycle->labels()->attach( array( $Label ) , [ 'count' => $Count ] ); } + event(new CycleSaved($Cycle)); return MessageResponse::success( trans( 'messages.success' ) ); } diff --git a/app/Listeners/UpdateLabelsCycleNumber.php b/app/Listeners/UpdateLabelsCycleNumber.php index a709a8f6..3fdda914 100644 --- a/app/Listeners/UpdateLabelsCycleNumber.php +++ b/app/Listeners/UpdateLabelsCycleNumber.php @@ -23,6 +23,7 @@ class UpdateLabelsCycleNumber { $cycle = $event->cycle; $cycleNumber = $cycle->cycle_number; +// dd($cycle->labels); $cycle->labels()->update(['cycle_number' => $cycleNumber]); } diff --git a/app/Models/Cycle.php b/app/Models/Cycle.php index 5cfb406b..2fe28b51 100644 --- a/app/Models/Cycle.php +++ b/app/Models/Cycle.php @@ -15,16 +15,6 @@ class Cycle extends Model 'date', 'user_id', 'cycle_number', 'sterilizer_id', 'note', 'verified', 'unique_id' ]; - protected static function booted() - { - static::created(function ($cycle) { - event(new CycleSaved($cycle)); - }); - - static::updated(function ($cycle) { - event(new CycleSaved($cycle)); - }); - } public function user(): BelongsTo {