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

Laravel withChain数组链式任务报错:Attempt to assign property of non-object

问题分析与修复方案

兄弟,你的问题核心出在链式任务的构造方式上——你用字符串拼接生成了$new_jobs_array里的元素,但Laravel的withChain()方法需要接收的是实际的Job实例数组,而非字符串。当Laravel尝试处理这些字符串时,会把它们当成非对象来操作,自然就抛出了Attempt to assign property of non-object的错误。

错误代码的关键问题

你这段代码里的问题点非常明显:

$new_job = 'new BootStrapWasNode('. $node .', '. $this->wasRequest .', '.$sourceJsonPath.')';
array_push($new_jobs_array, $new_job);

这里你把Job的实例化逻辑拼成了字符串,但withChain()需要的是已经实例化好的BootStrapWasNode对象,不是一段字符串形式的代码。

修复后的完整代码

把链式任务的构造逻辑改成直接实例化Job对象即可:

$new_jobs_array = [];
foreach ($this->wasRequest->nodes->sortByDesc('pivot.node_type') as $node) {
    if ($node->pivot->node_type != 'WAS_DMGR') {
        $snode = strtolower($node->hostname);
        $shortname = strtok($snode, '.');
        $fileName = strtolower($mnemonic).'_'.$shortname.'_'.$reqId.'.json';
        $sourceJsonPath = base_path() . "/json/was/" . $fileName;
        
        // 直接实例化Job对象,而非拼接字符串
        $new_job = new BootStrapWasNode($node, $this->wasRequest, $sourceJsonPath);
        array_push($new_jobs_array, $new_job);
    } else {
        $dmgr_node = $node;
    }
}

$dmgr_node_sname = strtok($this->wasRequest->nodes->where('pivot.node_type', 'WAS_DMGR')->pluck('hostname')[0], '.');
$fileName = strtolower($mnemonic).'_'.$dmgr_node_sname.'_'.$reqId.'.json';
$sourceJsonPath = base_path() . "/json/was/" . $fileName;

$this->wasRequest->status = 'Bootstrapping Nodes';
$this->wasRequest->save();

// 现在withChain接收的是正确的Job实例数组
BootStrapWasNode::withChain($new_jobs_array)->dispatch($dmgr_node, $this->wasRequest, $sourceJsonPath);

额外注意事项

  1. 确保BootStrapWasNode Job类的构造函数能正确接收并存储参数:
// 在BootStrapWasNode.php中
public function __construct($node, $wasRequest, $sourceJsonPath)
{
    $this->node = $node;
    $this->wasRequest = $wasRequest;
    $this->sourceJsonPath = $sourceJsonPath;
}
  1. 确认传入的参数是可序列化的:Laravel队列会序列化Job对象,所以$node、$this->wasRequest这类Eloquent模型实例没问题,但如果是自定义对象,需要确保它实现了Serializable接口,否则会出现序列化失败的问题。

这样修改后,链式任务就能正确传递数据,不会再触发非对象赋值的错误了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:06:26