php - 尝试在 null 上读取属性“total_product_quantity”
问题描述
我制作了一个嵌套循环,我创建了一个视图来显示我的数据。如何在视图中显示单个查询的数据。 这是我的查看页面
@foreach($products1 as $item1) <!-- first product loop -->
<tr>
<td>{{++$i}}</td>
<td>{{$item1->productname}}</td>
@foreach($products2 as $item2) <!-- second product loop -->
@if($item1->id == $item2->id) <!-- campare with second one -->
@for($k=1;$k<=31;$k++) <!-- loop for days -->
<td id="">
{{$log =App\Models\Production_log::select('total_product_quantity')->where('product_id',$item2->id)->where('employee_id','3')->where('log_date',$k.'-'.'12'.'-'.'2020')->first() }}
</td>
@endfor
@endif
@endforeach
</tr>
@endforeach
这是控制器代码
public function index($id)
{
$products1 = Product::get();
$products2 = Product::get();
$data['dispatch'] = Production_log::where('employee_id','=',$id)->get();
$dispatch = '01'.'-'.'12'.'-'.'2020';
return view('production.index',compact('products1','products2','data'));
}
错误消息
我的结果查询
{{$log =App\Models\Production_log::select('total_product_quantity')->where('product_id',$item2->id)->where('employee_id','3')->where('log_date',$k.'-'.'12'.'-'.'2020')->first() }}
**这是我的原始输出**
{"total_product_quantity":"6"}
我需要这个输出
{{$log->total_product_quantity}}
查看输出
5
解决方案
只需select('total_product_quantity')
在$log
分配时删除
推荐阅读
- algorithm - 根据“语法”或“上下文”相关性确定短语的“优点”
- python-3.x - 将 pandas Pivittable 导出到 excel 并进行聚合
- org-mode - 如何只导出代码块的结果而不是生成它的代码
- postgresql - jdbcTemplate 查询仅使用 SELECT * 执行
- csvhelper - *阅读*具有不同格式的多个部分的 CSV 文件的最佳实践
- sql-server - 用以前的记录值更新最新记录
- javascript - Switch 语句返回不正确的值
- go - 如何确认来自 Go 客户端的 gRPC 流量是 TLS 加密的
- python - 从谷歌驱动器获取 CSV,然后加载到熊猫
- excel - 如何阻止数据透视表过滤破坏我的 14 天滚动平均度量?