首页 > 解决方案 > 尝试在 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

标签: phplaravellaravel-5eloquent

解决方案


只需select('total_product_quantity')$log分配时删除


推荐阅读