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

devshane's avatar

Prevent serialization of models in queued Notification

I have an application with multiple schemas (it's a Multi-Tenant application).

I'm having some issues with Queueing because of serialization of models (and then the application looking in the public schema rather than the proper schema for the job). For regular Jobs, it was solved by not serializing models, but I see no such option for notifications. Right now, I've only thought of very dirty solutions (creating a Job that doesn't serialize that dispatches a notification synchronously) or worse, dispatching the notification immediately.

Is there a way for me to avoid model serialization in notifications that isn't apparent in the documentation or when tracing the code?

0 likes
1 reply
devshane's avatar

I found a good solution while digging through the code of a popular multi-tenant suite for Laravel that did the trick. (Credit: https://github.com/tenancy/multi-tenant) and specificallly this trait (https://github.com/tenancy/multi-tenant/blob/1380789375bb3eb75dbb4f951a44ec1876e5b5ee/src/Queue/TenantAwareJob.php). I had to heavily modify it for my own multi tenancy solution but it basically looked like:

<?php

/**
 * Serialization overrides to save and set tenant on sleep/wakeup
 */
namespace App\Lib\Queue;

use Illuminate\Queue\SerializesModels;
trait TenantAwareJob
{
    protected $tenant;
    use SerializesModels {
        __sleep as serializedSleep;
        __wakeup as serializedWakeup;
    }
    public function __sleep()
    {
        $this->tenant = getTenant(); //a global helper I've created for getting the currently selected tenant schema
        $attributes = $this->serializedSleep();
        return $attributes;
    }
    public function __wakeup()
    {
        setTenant($this->tenant); //a global helper I've created for setting the tenant schema

        $this->serializedWakeup();
    }

}```

Please or to participate in this conversation.