【问题标题】:How to resolve "Trying to get property 'pros_name' of non object" in laravel?如何解决laravel中的“试图获取非对象的属性'pros_name'”?
【发布时间】:2019-11-05 10:34:41
【问题描述】:

我的刀片中有以下 foreach 循环显示错误:

试图获取非对象的属性“pros_name”

我该如何解决?

@foreach ($reports as $report)
    <tr>
        <td>{{ $report->room_no }}</a></td>
        <td>{{ $report->room_type }}</td>
 @if($report->book_date== null || ($report->book_date != null && ($report->release_date != null && $report->release_date < date('Y-m-d'))))
        <td class="text-danger"><strong>Vacant</strong></td>
        <td>{{ $report->price }}</td>
                                <td>---</td>
                                <td></td>
                                @endif
                                @if($report->book_date != null && ( $report->release_date ==null || $report->release_date >= date('Y-m-d')))
                                    @php 
                                        $doc = DB::table('gen_resident_room')
                                            ->Join('sales_pipeline', 'gen_resident_room.person_id', '=', 'sales_pipeline.id')
                                            ->where([['room_id',$report->room_id]])->first();
                                            $n = explode (",",$doc->pros_name); //error
                                    @endphp
                                    @if($doc->stage === "MoveIn")
                                        <td class="text-success"><b>Occupied</b></td>
                                    @else
                                        <td class="text-success"><b>Booked</b></td>
                                    @endif
                                    <td>{{ $report->price }}</td>
                                    <td>{{ $doc->price }}</td>
                                    <td>{{ $n[0] }} {{ $n[1] }} {{ $n[2] }}</td>
                                @endif
                            </tr>
                            @endforeach

【问题讨论】:

  • 尝试使用 print_r($doc) 打印 $doc,是否包含数据?
  • 确保$doc确实存在并包含数据
  • 这意味着$doc不是一个对象,这意味着你不能用箭头语法访问它的值。填充$doc 的查询可能会返回一个数组或布尔值来表示查询失败。查看$doc 包含什么,以及DB::table 可以返回什么。 (顺便说一句,在视图中进行数据库查找并不完全理想,我建议将其移至控制器)

标签: php if-statement laravel-5 explode foreach-loop-container


【解决方案1】:

请尝试检查 $doc 是否包含数据。似乎文档是空的,所以它的脚本执行失败并抛出错误。

您也可以验证代码,例如:

if(!empty($doc))
{
 // Do you stuff
}

if(isset($doc->pros_name)) {
     $n = explode (",",$doc->pros_name);
}

【讨论】:

  • 我这样做了:if(isset($doc-&gt;pros_name)) { $n = explode (",",$doc-&gt;pros_name); } 但是它显示了一个新错误“试图获取非对象的属性“阶段”
  • 你检查$doc是否包含数据?
  • 如果$doc没有数据,那么所有关于$doc的实例都不能工作
  • 是的,我检查过它包含数据。 dd($doc); 显示存在的数据。
  • 那么检查阶段是否存在或者是否有任何值或为空?您需要为阶段创建另一个验证以检查是否为空
猜你喜欢
  • 2019-03-11
  • 2017-03-02
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
  • 2020-03-01
  • 2018-02-21
  • 2015-09-22
  • 2017-03-20
相关资源
最近更新 更多