0
votes

Im building a laravel app

@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

Basically I cant access the $review->child_id property it throws this error

Undefined property: Illuminate\Database\Eloquent\Builder::$child_id (View: /var/www/html/projects/guest-book/resources/views/reviews/comment.blade.php) (View: /var/www/html/projects/guest-book/resources/views/reviews/comment.blade.php)

but if I var dump it is there and I used it in previous if statement and it works just fine there.

What I am trying to do is display comment section each comment has child and parent ids. If comment does not have child id it is displayed normally otherwise it is indented.

How can I re-instantiate the variable in blade and how come I cant access that property which was used before in the same blade?

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) all reviews are basically like the one above so I didnt open them.

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"
}

Controller index method :

    public function show() {    
    $reviews = Review::
        where('deleted_at', NULL)
        ->where('parent_id', 0)
        ->paginate(12);
    return view('reviews.reviews', compact('reviews'));
}
2
Please show results of {{ dd($reviews) }}Alexey Mezenin
@AlexeyMezenin edited questionzerociudo
You've shown results for the first $review only.Alexey Mezenin
it's array type use it like this $review['child_id']Gaurav Gupta
@GauravGupta That one throws Cannot use object of type Illuminate\Database\Eloquent\Builder as array zerociudo

2 Answers

1
votes

You need to check child_id more explicitly

@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
votes

Check review is empty or not-

@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

If you are using laravel 5.5, then you can use optional() helper method-

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