I am using a command to execute an event to store some data to the database. But when executing the command, I get an error:
Illuminate\Contracts\Encryption\DecryptException : The payload is invalid.
When executing this event in a controller, everything works fine. The problem seems to be in the listener, when I am using array_push
:
array_push($reports, $event->type);
$reports
is an existing array, and I am adding one more report_type
to that array, and then saving it to the database:
$event->budget->update(['reported' => $reports]);
Also, the reported
field is an array field, as declared in my model:
protected $casts = [
'reported' => 'array'
];
My question is, why does it work when executing that event from the controller, but not when using a command? And ofcourse, a fix for this would be welcome, but my main question is, that I am trying to understand the error.
If more information is needed, I will add it accordingly.
In the command I do the following:
foreach (reports() as $report) {
$budgets->each(function ($budget) use ($report) {
return event(new BudgetReported($budget, $report));
});
}
My event:
class BudgetReported {
use SerializesModels;
public $budget;
public $type;
/**
* Create a new event instance.
*
* BudgetReport constructor.
* @param Budget $budget
* @param $type
*/
public function __construct(Budget $budget, $type)
{
$this->budget = $budget;
$this->type = $type;
}
}
And in my listener:
public function handle(BudgetReported $event)
{
$reports = $event->budget->reported;
$reports == null ? $reports = [] : $reports;
array_push($reports, $event->type);
$event->budget->update(['reported' => $reports]);
}
Hope that helps!