Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devinfd's avatar
Level 13

Unable to serialize object property on a queued job

I've been fighting an issue for the past week that I can't find a resolution to.

I have a queued job that has an object (not a model) as constructor argument. The object has property that is a model with a coordinates attribute. The coordinates are a custom Point cast using the MatanYadaev/laravel-eloquent-spatial package.

something like:

class Report
{
  $account = null;
  public function __construct(Model $account) {
    $this->account = $account
  }
}

$account = Account::first();
MakeReport::dispatch(new Report($account));

The coordinates attribute is properly casted on the Account model

use \MatanYadaev\EloquentSpatial\Objects\Point
Account extends Model {
  protected $casts = [
      'coordinates' => Point::class
  ];
}

The problem is that the coordinates are not being serialized by laravel and I get the error: Illuminate\Queue\InvalidPayloadException Unable to JSON encode payload. Error code: 5

Any ideas? I'm starting to lose my mind on this.

PHP 8.1.10 Laravel 9.28.0

0 likes
5 replies
kevinbui's avatar

Is that gonna work if you stop casting the coordinates field? Can you show some code from the Point class too?

Does your MakeReport job using the SerializesModels trait? Then it simply including the model id instead of serializes the whole model object.

kevinbui's avatar

We are looking at the source code. In the mean time, you might simply unset that attribute. It will get reloaded anyway.

MakeReport::dispatch(
    new Report(tap($account)->setAttribute('coordinates', null))
);
devinfd's avatar
Level 13

I may have narrowed this down to a smaller issue. A model with a geospatial attribute passed to a job causes the Illuminate\ Queue\ InvalidPayloadException Unable to JSON encode payload. Error code: 5 error because it is binary data. When Laravel does json_encode on the queable object it fails.

https://laravel.com/docs/9.x/queues#handle-method-dependency-injection

Binary data, such as raw image contents, should be passed through the base64_encode function before being passed to a queued job. Otherwise, the job may not properly serialize to JSON when being placed on the queue.

1 like
devinfd's avatar
devinfd
OP
Best Answer
Level 13

FYI to anybody who sees this in the future: I found a solution. The miscellaneous object with a model property needs to use the SerializesModels trait.

2 likes

Please or to participate in this conversation.