Laravel eloquent 问题,请大家看看下面的两端代码,为什么结果不同!感谢!
第一段代码
$all_lines = $company->company_status()->where('quarter', $decision_quarter - 1)->first()->com_sta_line_det;
foreach ($all_lines as $line) {
$decision->com_dec_line()->create([
'company_line_id' => $line->company_line_id,
'company_line_sale' => 0,
]);
}
第二段代码
$all_lines = $company->company_status->where('quarter', $decision_quarter - 1)->first()->com_sta_line_det;
foreach ($all_lines as $line) {
$decision->com_dec_line()->create([
'company_line_id' => $line->company_line_id,
'company_line_sale' => 0,
]);
}
原问题是:第一段代码有括号 “->company_status ()->” 可以运行,第二段代码没有括号的无法运行(系统跳过了 foreche 并且没有报错),两段代码为什么结果不同呢?
在大家的指点下证明了,第二段代码 $all_lines 查询到的是一个空集合,已经解决了该问题,感谢大家!
但是,另外一个地方与第二段代码相同的写法,为什么可以取到数据呢?????代码如下:
public function technology(Company $company, $status)
{
$technology = $company->company_status->where('quarter', $status)->first()->com_sta_technology;
return $technology;
}
$company->company_status()->where(XXX)
,这里是在 Builder 实例上调用 where 方法,也就是 sql 中的 where。$company->company_status->where(XXX)
,这里是在 Collection 实例上调用 where。