我热衷于使我的代码解耦并准备好进行测试。Laravel将哨兵用户注入模型
我有一个Eloquent模型getBudgetConvertedAttribute取决于sentry用户属性。
public function getBudgetConvertedAttribute()
{
return Sentry::getUser()->currency * $this->budget;
}
测试时抛出错误,因为Sentry :: getUser返回null。
我的问题是,我应该如何编码从控制器或服务提供者绑定或测试中将用户注入模型?
我也这么认为。但是雄辩模型使用类似静态方法的外观。例如。类名::有() - >其中() - > get()方法 - >限制();在这种情况下,它抛出错误,说建设期望Sentry,但它通过null。我仍在进一步研究如何使其工作 –
查询可能有问题。你尝试dd(Sentry :: getUser() - > currency * $ this-> budget);看到输出?你得到的错误/异常是什么(如果有的话)? – Ruffles