工匠队列在具有变量值的类中工作

I would like to queue the following pdo statement to create a database like this:

    Queue::push(function($job, $dbname){
        $dbh = new PDO("mysql:host=".getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'));

        $dbh->exec("CREATE DATABASE `$dbname` CHARACTER SET=utf8 COLLATE=utf8_unicode_ci;
            GRANT ALL ON `$dbname`.* TO 'xxx'@'%';
            FLUSH PRIVILEGES;");

    unset($dbh);
        Log::useFiles(storage_path().'/logs/databasecreated');
        Log::info('database created: '.$dbname);
        $job->delete();
    });

But it throws

'Missing argument 2 for Jeremeamia\SuperClosure\SerializableClosure::{closure}()

when trying to run php artisan queue:work

$dbname

is set like

$dbname = $this->getDbName();

and the entire code resides in a class' function.

I've also tried to use $this->getDbName() and $this->dbName directly within the closure, but that naturally didn't work.

Isn't it possible to run commands like this in a queue?

Try passing the variable with a use statement:

Queue::push(function($job) use ($dbname){
    $dbh = new PDO("mysql:host=".getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'));

    $dbh->exec("CREATE DATABASE `$dbname` CHARACTER SET=utf8 COLLATE=utf8_unicode_ci;
        GRANT ALL ON `$dbname`.* TO 'xxx'@'%';
        FLUSH PRIVILEGES;");

    unset($dbh);
    Log::useFiles(storage_path().'/logs/databasecreated');
    Log::info('database created: '.$dbname);
    $job->delete();
});