【发布时间】:2025-12-31 00:20:12
【问题描述】:
我有一份简单的工作:
class SampleJob extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels, DispatchesJobs;
private $customer;
public function __construct(Customer $customer)
{
parent::__construct();
$this->customer = $customer;
}
public function handle()
{
doSomeStuff($customer->currency());
}
}
在Customer 模型中,我与country 有关系:
clas Customer extends Model
{
public function country()
{
return $this->belongsTo(Country::class);
}
public function currency()
{
return $this->country->currency();
}
}
当我通过 dispatch(new SampleJob($customer)) 发送作业时,我在日志中收到错误:
Call to a member function currency() on null {"code":0,"file":"Customer.php","line":386}
这里有一个棘手的问题是,如果我从工作中删除 SerializesModels 特征,它会正常工作。但我真的不想这样做,因为它会导致无法预料的错误(我们有很多这样的工作,还有更多的属性)。
所以我只想弄清楚为什么会发生这个错误。
我使用database 驱动程序来工作。
Laravel 5.8.16。此外,使用 Laravel 5.6 也可以正常工作。
【问题讨论】: