John Davidson

php - Laravel Model event not firing

0 comments
Message:


The Notifications table has status column which stores boolean value. i wanted to update the status column to true when the job in the queue has run successfully. Doing dd() in listener shows output but the column is not updating in db. Can someone help on this?? Any help is much appreciated. Thanks in Advance


Job


class SendEmailJob implements ShouldQueue
{
public function handle()
{
Notification::send($this->user, new courseAddedNotification($this->msg));
JobFailStatus::dispatch($this->user);
}
}

Event


class JobFailStatus
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public $obj;

public function __construct($status)
{
$this->obj = $status;
}
}

Listener


use App\Models\Notifications;
class SendStatus
{
public function __construct()
{
//
}

public function handle(JobFailStatus $event)
{
//dd($event->obj->status);
Notifications::updating([
'status'=>'true'
]);
}
}

dd(Notifications::all()); in listener handle() gives the below data


Illuminate\Database\Eloquent\Collection^ {#1572
#items: array:6 [
0 => App\Models\Notifications^ {#1569
#table: "notifications"
#fillable: array:6 [
0 => "notificationTypesId"
1 => "notificationTypeName"
2 => "userId"
3 => "email"
4 => "status"
5 => "recipientName"
]
#connection: "pgsql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [
"id" => 55
"notificationTypesId" => 1
"notificationTypeName" => "course Added"
"userId" => 1
"email" => "[email protected]"
"recipientName" => "chris"
"status" => false
"created_at" => "2021-08-17 12:25:32"
"updated_at" => "2021-08-26 17:21:00"
]
#original: array:9 [
"id" => 55
"notificationTypesId" => 1
"notificationTypeName" => "course Added"
"userId" => 1
"email" => "[email protected]"
"recipientName" => "chris"
"status" => false
"created_at" => "2021-08-17 12:25:32"
"updated_at" => "2021-08-26 17:21:00"
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
#enableLoggingModelsEvents: true
#oldAttributes: []
}


My Answer:
The issue seems to be with the way you are trying to update the status column in the Notifications table. Instead of using `updating()` method, you should use the `update()` method to update the status column.

Here is the updated code for your Listener:

php
use App\Models\Notifications;

class SendStatus
{
public function __construct()
{
//
}

public function handle(JobFailStatus $event)
{
Notifications::where('id', $event->obj->id)->update([
'status' => true
]);
}
}


In this code, we are using the `update()` method to update the status column in the Notifications table based on the id of the object passed in the event.

Make sure to pass the id of the object correctly in the JobFailStatus event constructor and update the code accordingly.

After making these changes, the status column should be updated to true in the Notifications table when the job in the queue has run successfully.

Rate this post

5 of 5 based on 4500 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us