多个循环中的Laravel刀片未定义变量

时间:2018-01-30 07:06:41

标签: php laravel

我正在构建一个laravel应用程序

@foreach ($reviews as $review)
<div>
    @if ($review->child_id == null)
    ... display data  
    @else
    {{$review->child_id}}
        @while ($review->child_id)   // this line throws error
        ...display data
        @php
            //trying to instantiate variable again       
            $review = App\Review::where('id',$review->child_id); // this line doesnt work
        @endphp
        @endwhile
    @endif
</div>
@endforeach

基本上我无法访问$ review-&gt; child_id属性,它会抛出此错误

  

未定义属性:Illuminate \ Database \ Eloquent \ Builder :: $ child_id(查看:/var/www/html/projects/guest-book/resources/views/reviews/comment.blade.php)(查看:/ var /www/html/projects/guest-book/resources/views/reviews/comment.blade.php)

但如果我转储它就在那里,我在之前的if语句中使用它,它在那里工作得很好。

我要做的是显示评论部分,每个评论都有子和父ID。 如果注释没有子ID,则正常显示,否则缩进。

如何在刀片中重新实例化变量,为什么我无法访问之前在同一刀片中使用过的属性?

dd($ review):

Review {#248 ▼
  #fillable: array:6 [▶]
  #dates: array:1 [▶]
  #connection: "mysql"
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:11 [▼
    "id" => 4
    "grav_url" => "https://www.gravatar.com/avatar/25d755d39ba840f931b70f90cbd591eb?d=https%3A%2F%2Fwww.gravatar.com%2Favatar%2F&s=20"
    "full_name" => "user name"
    "review" => "answer"
    "ip_address" => "127.0.0.1"
    "deleted_at" => null
    "parent_id" => "0"
    "child_id" => "5"
    "user_id" => null
    "created_at" => "2018-01-29 14:09:16"
    "updated_at" => "2018-01-29 14:09:16"
  ]
  #original: array:11 [▶]
  #changes: []
  #casts: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
  #forceDeleting: false
}

dd($ reviews)所有评论基本上都与上面的评论一样,所以我没有打开它们。

LengthAwarePaginator {#242 ▼
  #total: 4
  #lastPage: 1
  #items: Collection {#244 ▼
    #items: array:4 [▼
      0 => Review {#245 ▶}
      1 => Review {#246 ▶}
      2 => Review {#247 ▶}
      3 => Review {#248 ▶}
    ]
  }
  #perPage: 12
  #currentPage: 1
  #path: "http://localhost:8000"
  #query: []
  #fragment: null
  #pageName: "page"
}

控制器索引方法:

    public function show() {    
    $reviews = Review::
        where('deleted_at', NULL)
        ->where('parent_id', 0)
        ->paginate(12);
    return view('reviews.reviews', compact('reviews'));
}

2 个答案:

答案 0 :(得分:1)

您需要更明确地检查child_id

@foreach ($reviews as $review)
<div>
@if ($review->child_id == null)
... display data  
@else
{{$review->child_id}}
    @while (optional($review)->child_id)   // change the if
    ...display data
    @php
        //trying to instantiate variable again       
        $review = App\Review::where('id',$review->child_id)->first(); //add first
    @endphp
    @endwhile
@endif
</div>
@endforeach

答案 1 :(得分:1)

检查review是否为空 -

@else
   @while ($review!=null&&$review->child_id!=null)
      ...display data
   @php
        //trying to instantiate variable again       
        $review = App\Review::where('id',$review->child_id)->first(); 

   @endphp
   @endwhile
@endif

如果您使用的是laravel 5.5,则可以使用optional()辅助方法 -

@while (optional($review)->child_id)