模型的 save() 方法无法使用队列?
目的:我想要异步 save()
Controller:
$model = User::find(1);
dispatch(new ModelSave($model));
Job
public function handle()
{
//
$this->model->save();
}
Job exception
Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Models\Device]. in E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php:452
Stack trace:
#0 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\SerializesAndRestoresModelIdentifiers.php(85): Illuminate\Database\Eloquent\Builder->firstOrFail()
#1 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\SerializesAndRestoresModelIdentifiers.php(55): App\Jobs\ModelSave->restoreModel(Object(Illuminate\Contracts\Database\ModelIdentifier))
#2 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\SerializesModels.php(45): App\Jobs\ModelSave->getRestoredPropertyValue(Object(Illuminate\Contracts\Database\ModelIdentifier))
#3 [internal function]: App\Jobs\ModelSave->__wakeup()
#4 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\CallQueuedHandler.php(42): unserialize('O:18:"App\\Jobs\\...')
#5 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\Jobs\Job.php(83): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\DatabaseJob), Array)
#6 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(327): Illuminate\Queue\Jobs\Job->fire()
#7 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(277): Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Queue\WorkerOptions))
#8 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(118): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\DatabaseJob), 'database', Object(Illuminate\Queue\WorkerOptions))
#9 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(102): Illuminate\Queue\Worker->daemon('database', 'modelsave', Object(Illuminate\Queue\WorkerOptions))
#10 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(86): Illuminate\Queue\Console\WorkCommand->runWorker('database', 'modelsave')
#11 [internal function]: Illuminate\Queue\Console\WorkCommand->handle()
#12 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(29): call_user_func_array(Array, Array)
#13 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#14 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#15 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Container\Container.php(572): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#16 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Console\Command.php(183): Illuminate\Container\Container->call(Array)
#17 E:\server\wwwroot\xiaohoumengqi\vendor\symfony\console\Command\Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#18 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Console\Command.php(170): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#19 E:\server\wwwroot\xiaohoumengqi\vendor\symfony\console\Application.php(1009): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#20 E:\server\wwwroot\xiaohoumengqi\vendor\symfony\console\Application.php(273): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 E:\server\wwwroot\xiaohoumengqi\vendor\symfony\console\Application.php(149): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#22 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Console\Application.php(89): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#23 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(122): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#24 E:\server\wwwroot\xiaohoumengqi\artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#25 {main}
对模型原理不懂,难道要在队列 重新new
或者 重新查找
,重新赋值,再重新save
吗?
本作品采用《CC 协议》,转载必须注明作者和本文链接
见:『找不到』异常
你调用了
firstOrFail
findOrFail
之类的错误的代码应该不是你贴出的代码。
@lyxxxh 这是队列失败记录的日志,这就是全部的代码了,就很普通的
save
,只是我想用队列异步处理看错误信息,你的findFirst就找不到model,model不存在,后面怎么能save
因为 Model 在放队列的时候自动序列化了,所以不能在队列里进行保存等操作。
你试试把队列的 SerializesModels 删掉试试呢。
队列每次都会重新从数据库查找,所以你需要每次在队列里赋值然后保存
可能开启了事务没提交。
@taohua 就代码所示的一个
save()
,没有其它操作了控制器
Job
@xiaohuasheng 有尝试过 new Order; 吗?我是 new 的时候报这个错,即使是在队列里面新增都不行, 控制器
job
@白小二 可能是因为这句话吧,一个新new的模型无法放入构造函数里
@xiaohuasheng 那是不是意味着我得采用传
类名+创建|更新+数据
这种形式了,按照这译文,构造函数不能接收新的模型啊参考 最佳答案
@24K大白羊 大神,终于搞定了,我太迟钝了,在看了 @xiaohuasheng 的回答,我才知道你说的是啥 :joy:
@LiamHao 感谢