0

I'm trying to get the properties from the array returned by get method of Eloquent. It is a nested array as you see in image

I have tried the following answers from stackoverflow.

Property [title] does not exist on this collection instance ,

Trying to get property of non-object (View: C:\xampp\htdocs\travel\resources\views\user\profile.blade.php)

Looping PHP Nested Arrays - Extract values into Blade Views (Laravel)

My blade code is

 @if(isset($reportRP))
 @foreach($reportRP as $rrp)
  <tr>
<td>{{ $rrp['name'] }}</td>
<td>{{$rrp['reports']->id }}</td>
 @endforeach
 @endif

Collection returned by Eloquent get method

zmag
  • 7,825
  • 12
  • 32
  • 42
Abrar Ahmad
  • 330
  • 8
  • 15

3 Answers3

0

Maybe:

@if(isset($reportRP))
    @foreach($reportRP as $rrp)
       <tr>
       <td>{{ $rrp['name'] }}</td>
       @foreach($rrp['reports'] as $report)
           <td>{{ $report['id'] }}</td>
       @endforeach
        </tr>
     @endforeach
@endif
blinkofaneye
  • 148
  • 10
  • Thanks, @blinkofaneye. Yeah, that's working. I'm showing it in a table where each patient name has report. If reports are multiple, the patient name will be repeated with each report. But this code shows all reports in same . Does it make sense? – Abrar Ahmad Apr 23 '19 at 20:22
  • You're welcome. That's wierd it should generate a for each report xD – blinkofaneye Apr 23 '19 at 20:29
  • Thank I just figure it out from your answer. – Abrar Ahmad Apr 23 '19 at 20:36
0

$rrp['reports'] is an array of items. You'll have to specify from which object you'll want the id property

PtrTon
  • 3,705
  • 2
  • 14
  • 24
0

Anyway you can use Laravel function optional and if your object is empty it will return null

$rrp['name']->name ----- Will return error if $rrp['name'] is null

optional($rrp['name'])->name ----- Will return null if $rrp['name'] is null

Community
  • 1
  • 1
Levon Babayan
  • 266
  • 2
  • 18