1

I am using Laravel Notification to notify users about certain things. Everything works as expected but there are certain cases where I get the next error.

(1/1) InvalidPayloadException
Unable to JSON encode payload. Error code: 5

The thing that I noticed when I get these errors is that the users being notified have data in a BLOB column in the users table. My guess is that for some reason Laravel is not being able to serialize the values contained in the BLOB column and as a result of that I get the error shown above.

Actually, if I change the values of that column to NULL the notification process works perfectly. I tried to use the hidden and cast properties in the User model but it doesn't help.

Reading the documentation I haven't found the way to change that serialization so I can make it work. Is there anything I can to change this behavior?

I appreciate your comments. Thanks in advance!

1 Answers1

0

As you have found by yourself JSON does not accept binary data, the trick here is to encode the BLOB to base64 so it can be used in a JSON, the only downside is that you have to decode it when you retrieve the JSON.

You can use an accessor on your model to be able to auto encode the Blob field when the Model is serialized in JSON, i.e.:

// in User Model
public function getBlobAttribute($value)
{
    return base64_encode($value);
}

You only have to remember to decode the BLOB when you use it as real binary data (for example if it's an image), i.e.:

$blob = base64_decode($model->blob)

Or you can write a method that retrieve the blob without using the accessor:

// in User Model
public function getRawBlob()
{
    $this->getOriginal('blob');
}

I Hope this help.

dparoli
  • 8,891
  • 1
  • 30
  • 38