You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel队列报错:调用不完整对象方法问题求助

Laravel Queue 任务执行报错:调用不完整对象的方法

错误信息

Error: The script tried to call a method on an incomplete object. Please ensure that the class definition "App\Jobs\SeedingCustomerData" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide an autoloader to load the class definition in /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php:117
Stack trace:
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(117): method_exists()
#1 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(70): Illuminate\Queue\CallQueuedHandler->dispatchThroughMiddleware()
#2 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(98): Illuminate\Queue\CallQueuedHandler->call()
#3 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(428): Illuminate\Queue\Jobs\Job->fire()
#4 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(378): Illuminate\Queue\Worker->process()
#5 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(172): Illuminate\Queue\Worker->runJob()
#6 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(117): Illuminate\Queue\Worker->daemon()
#7 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(101): Illuminate\Queue\Console\WorkCommand->runWorker()
#8 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\Queue\Console\WorkCommand->handle()
#9 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Util.php(40): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#10 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure()
#11 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod()
#12 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(653): Illuminate\Container\BoundMethod::call()
#13 /var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php(136): Illuminate\Container\Container->call()
#14 /var/www/html/vendor/symfony/console/Command/Command.php(298): Illuminate\Console\Command->execute()
#15 /var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php(121): Symfony\Component\Console\Command\Command->run()
#16 /var/www/html/vendor/symfony/console/Application.php(1040): Illuminate\Console\Command->run()
#17 /var/www/html/vendor/symfony/console/Application.php(301): Symfony\Component\Console\Application->doRunCommand()
#18 /var/www/html/vendor/symfony/console/Application.php(171): Symfony\Component\Console\Application->doRun()
#19 /var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php(94): Symfony\Component\Console\Application->run()
#20 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\Console\Application->run()
#21 /var/www/html/artisan(37): Illuminate\Foundation\Console\Kernel->handle()
#22 {main}

相关代码

控制器代码

public function processing_customer_subscription(Request $request)
{
  try {
    $token = $this->generateSeedingToken();
    $customer = Customer::where('email', $request->input('email'))->orWhere('id', $request->input('user_id'))->first();
    if ($customer == null)
      throw new \Exception('data customer tidak ditemukan', 422);

    $module = Module::where('code', $request->input('code_module'))->first();
    $package = Package::where([
      'code' => $request->input('code_package'),
      'module_id' => $module->id
    ])->first();

    $check_sub = CustomerSubscription::where([
      'billing_number' => $request->input('billing_number'),
      'customer_id' => $customer->id
    ])->count();

    if ($check_sub > 0) {
      // TODO
      $subs = CustomerSubscription::where('billing_number', $request->input('billing_number'))->first();
      $this->response['data'] = $subs;
      $this->response['msg']  = 'berhasil';
    } else {
      $db = DB::transaction(function () use ($request, $customer, $module, $package, $token) {
        // create subscription data
        $subs = new CustomerSubscription();
        $subs->id = $this->generateRandomUuid();
        $subs->customer_id = $customer->id;
        $subs->module_id = $module->id;
        $subs->package_id = $package->id;
        $subs->billing_number = $request->input('billing_number');
        $subs->start_date = $request->input('start_date');
        $subs->end_date = $request->input('end_date');
        $subs->max_user = $package->max_user;
        $subs->max_data = $package->max_data;
        $subs->contract_value = $request->input('total_price');
        $subs->payment_status = 'PAID';
        $subs->status = 'ACTIVE';
        $subs->save();

        return $subs;
      });

      SeedingCustomerData::dispatch($customer->code, $token)->afterCommit();
      $this->response['data'] = $db;
      $this->response['msg']  = 'berhasil';
    }

    return $this->response;

  } catch (\Throwable $th) {
    $errorCode = ($th->getCode() > 99 && $th->getCode() < 600) ? $th->getCode() : 500;
    throw new \Exception($th->getMessage(), $errorCode);
  }
}

SeedingCustomerData 任务类代码

class SeedingCustomerData implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Common;
    private $code;
    private $token;

    public function __construct($code, $token)
    {
        $this->code = $code;
        $this->token = $token;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        set_time_limit(160);
        $customer = Customer::select('id','name','code','email','phone','password')->where('code', $this->code)->first();
       
        $dataToSeed = [
            'charter_categories' => CharterCategory::all(),
            'charter_delivery_statuses' => CharterDeliveryStatus::all(),
            'charter_value_categories' => CharterValueCategory::all(),
            'category_file' => CategoryFile::all(),
            'cost_resources' => CostResource::all(),
            'charter_tags' => CharterTag::all(),
            'charter_type_groups' => CharterGroupType::all(),
            'currencies' => Currency::all(),
            'partners' => Partner::all(),
            'pay_types' => PaymentType::all(),
            'risk_categories' => RiskCategory::all(),
            'risk_impacts' => RiskImpact::all(),
            'risk_statuses' => RiskStatus::all(),
            'stackholder_types' => StackholderType::all()
            // 'Language' => Language::all(),
            // 'Theme' => Theme::all()
        ];

        $dbTenant = DB::connection('tenant_master');

        try {
            $dbTenant->beginTransaction();

            foreach ($dataToSeed as $modelName => $data) {
                $this->seedData($dbTenant, $modelName, $data, $customer->code);
            }

            $dbTenant->commit();

            SeedingStatus::on('pgsql')->where('token', $this->token)
                ->update([
                    'seeding_end_time' => Carbon::now()->format('Y-m-d H:i:s'),
                    'is_successful' => true,
                ]);
        } catch (\Throwable $th) {
            $dbTenant->rollback();
        }
    }

    private function seedData($db, $modelName, $data, $tenantCode)
    {
        if (count($data) > 0) {
            $data->each(function ($record) use ($db, $modelName, $tenantCode) {
                unset($record->deleted_at);
                unset($record->id);
                $record->tenant_id = $tenantCode;
                $record->id = $this->generateRandomUuid();

                $db->table(strtolower($modelName))->updateOrInsert(
                    ['tenant_id' => $tenantCode],
                    $record->toArray()
                );
            });
        }
    }
}

问题现状

已检查类的自动加载配置、确认必要依赖已安装,但错误仍未解决,需要分析错误原因并提供修复方案。


错误原因分析

  1. 类命名空间缺失:报错提示的类是App\Jobs\SeedingCustomerData,但任务类代码中没有声明对应命名空间,导致自动加载器无法定位类定义,反序列化时生成不完整对象。
  2. 队列积压旧任务:如果任务类结构发生过修改(比如新增Common Trait),队列中之前入队的旧任务基于旧类结构序列化,反序列化时会因结构不匹配失败。
  3. 队列Worker未重启:Laravel队列Worker是常驻进程,修改类文件后不会自动重新加载类定义,导致Worker仍使用旧结构处理任务。
  4. Common Trait加载异常:Common Trait的文件路径、命名空间与use语句不匹配,导致类初始化时无法加载该Trait,类结构不完整。

修复方案

  1. 补充任务类命名空间与依赖引入
    在SeedingCustomerData类开头添加完整的命名空间和依赖声明:

    namespace App\Jobs;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Foundation\Bus\Dispatchable;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Queue\SerializesModels;
    // 根据实际路径补充Common Trait的命名空间
    use App\Traits\Common;
    
  2. 更新Composer自动加载映射
    执行命令刷新自动加载文件:

    composer dump-autoload
    
  3. 清除队列积压的旧任务
    清空所有未处理的旧任务,避免旧结构任务干扰:

    php artisan queue:clear
    
  4. 重启队列Worker
    重启Worker使其加载最新的类定义:

    php artisan queue:restart
    
  5. 验证Trait引入正确性
    确认Common Trait的文件存放路径、命名空间与类中的use语句完全一致,确保Trait能被正确加载。


内容的提问来源于stack exchange,提问作者elcicko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 03:44:55