0

Ok, so I have an array of objects created in my controller

$displayRooms[$room] = $this->getNextEvent($events, $room);

return view('schedule')->with(['displayRooms' => $displayRooms]);

Then in my view, I am able to iterate over the array and get the $room and $events

@foreach ($displayRooms as $room => $events)
    {{ var_dump($events) }}
@endforeach

The var_dump($events) produces

/home/vagrant/sites/google-cal/storage/framework/views/96faa8d8afe2e0f07db5d96a536009499a3c48a5.php:22:
object(stdClass)[176]
  public 'available' => boolean false
  public 'room' => string 'Room 318' (length=8)
  public 'summary' => string 'Chem 3090 Himmeldirk' (length=20)
  public 'startTime' => string '12:00pm' (length=7)
  public 'endTime' => string '3:00pm' (length=6)
  public 'startDay' => string '03/21/17' (length=8)
  public 'endDay' => string '03/21/17' (length=8)

/home/vagrant/sites/google-cal/storage/framework/views/96faa8d8afe2e0f07db5d96a536009499a3c48a5.php:22:null

/home/vagrant/sites/google-cal/storage/framework/views/96faa8d8afe2e0f07db5d96a536009499a3c48a5.php:22:null

/home/vagrant/sites/google-cal/storage/framework/views/96faa8d8afe2e0f07db5d96a536009499a3c48a5.php:22:
object(stdClass)[226]
  public 'available' => boolean false
  public 'room' => string 'Room 513 (Voinovich Room)' (length=25)
  public 'summary' => string 'Reynolds' (length=8)
  public 'startTime' => string '1:00pm' (length=6)
  public 'endTime' => string '3:00pm' (length=6)
  public 'startDay' => string '03/21/17' (length=8)
  public 'endDay' => string '03/21/17' (length=8)

My question is how can I iterate over the events object in Blade in order to access the properties of the object, so that I may pass them to a partial view?

1 Answer 1

1

If I'm understanding what you are asking (and your data structure) properly, then this should work. You can iterate the array of event objects to access each one individually, and then iterate each individual one to access each individual property / value. This code should be nested within your first foreach (replace the dump with it).

@foreach($events as $event) 
    @foreach ($event as $prop => $value) 
        // do whatever with prop / values
    @endforeach
@endforeach

If you don't need to access each property / value of the object one at a time, then I would recommend this instead.

@foreach($events as $event)
    // do whatever with each event
    {{ var_dump($event->available) }}
@endforeach
Sign up to request clarification or add additional context in comments.

7 Comments

Received this error message: Invalid argument supplied for foreach()
Are you sure that you nested the foreach within your existing one? Can you paste the exact code that you are running when it errors?
@foreach ($displayRooms as $room => $events) @foreach($events as $event) @foreach ($event as $prop => $value) {{ var_dump($value) }} @endforeach @endforeach @endforeach
Is $events not an array of objects?
Yes, But I think that it might be erroring out whenever it hits the 1st array that is null. See above paste of the var_dump()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.