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

Crazylife's avatar

Serialization of 'closure' is not allowed in Job

I am calling this function in my controller

 $response = $this->ajaxDispatch(new CreateOrder($request));

and then in my job

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

I am getting the error as shown in title when trying different local environment.

May i know what's the issue and how to solve it?

0 likes
7 replies
Talinon's avatar

@crazylife The request object is a complex object with a lot of data - some if that having closures. You don't want to serialize the entire request object because it is massive. Instead, just pass the specific data that the job needs. In many cases, this might just be a single model. If using the SerializesModels trait, its serialization will be further simplified to just it's primary id, which will get queried upon the job's execution.

Crazylife's avatar

If i just $request->query(), it returns me Serialization of 'class@anonymous' is not allowed in file /var/www/vendor/laravel/framework/src/Illuminate/Queue/Queue.php on line 147, Is it correct, i already pass specific params.

carltondickson's avatar

@crazylife if passing an array to your job as the parameter causes that error double check that the CreateOrder job itself doesn't have any errors in it - relating to importing classes etc

Is that the actual error message you get in your code?

Are you using PHP7.4 and using the new shorthand for writing functions?

Crazylife's avatar

In my Job

   public function __construct($request)
    {
        $this->request = $this->getRequestInstance($request);
    }

My job class

abstract class Job implements ShouldQueue
{
    use InteractsWithQueue;
    use Jobs;
    use Queueable;
    use Relationships;
    use SerializesModels;

    public function getRequestInstance($request)
    {
        if (!is_array($request)) {
            return $request;
        }

        $class = new class () extends FormRequest {
        };

        return $class->merge($request);
    }
}

The error happen after call $this->getRequestInstance($request);. And yes I am using PHP 7.4

carltondickson's avatar

Thanks for posting more code.

Feels like this anonymous function might be the issue?

        $class = new class () extends FormRequest {
        };

Is your import ok? Think it would be...

use Illuminate\Foundation\Http\FormRequest;

Also, is it possible to serialize closures? Might need to look into this in more detail

https://stackoverflow.com/questions/13734224/exception-serialization-of-closure-is-not-allowed/19730234#19730234

This looks like a really useful post, sounds very similar to what you are doing and offers a nice explanation

https://stackoverflow.com/a/46423879/682754

Crazylife's avatar

Yeah,

 $class = new class () extends FormRequest {
        };

If i am using $request->all(), then it will merge with the function above, then caused Serialization of 'class@anonymous' is not allowed.

If i am passing just $request, then it will just pass Serialization of Closure is not allowed.

Why some of the local environment able to proceed but some are not and throwing the error above?

App\Abstracts\FormRequest@anonymous {#1657
  #container: null
  #redirector: null
  #redirect: null
  #redirectRoute: null
  #redirectAction: null
  #errorBag: "default"
  #validator: null
  #json: null
  #convertedFiles: null
  #userResolver: null
  #routeResolver: null
  +attributes: Symfony\Component\HttpFoundation\ParameterBag {#1680
    #parameters: []
  }
  +request: Symfony\Component\HttpFoundation\ParameterBag {#1673
    #parameters: []
  }
  +query: Symfony\Component\HttpFoundation\InputBag {#1681
    #parameters: array:25 [
      "company_id" => 6
      "vendor_id" => 1

Please or to participate in this conversation.