Search code examples
phplaravellaravel-5laravel-collection

How to get the value from this structure of an Object in Laravel


enter image description here

I wanted to display the value of status field from the boardings table which is in a relationship with travel table. When I do this code:

$travel->boardings->status

it returns an error that says, $status is undefined.


Solution

  • As you can see boardings is of type Collection so you can use it with index

    $travel->boardings[0]->status
    

    or by using for loop

     foreach($travel->boardings  as $boarding){
            $status = $boarding->status;
    }