尝试访问Laravel 4中的关系数据时出错

I've been using Laravel 4 for a few days now and I stumbled across something I can't seem to fix.

Using Eloquent, I've set up 2 models, where Appointment has a belongsTo relationship to Room. After retrieving it like so (using eager loading):

$appointments = Appointment::with('room')->get();

I'm printing it in my view: http://paste.laravel.com/fnL

I loop through it and retrieve a value of Appointment:

@foreach($appointments as $appointment)
    <tr>
        <td>{{ $appointment->begins_at }}</td>
        <td>
            <a href="/admin/appointments/delete/{{ $appointment->id }}" class="btn btn-mini btn-danger">Verwijderen</a>
        </td>
    </tr>
@endforeach

This works fine, but when I add the following line:

<td>{{ $appointment->room->name }}</td>

It throws the following error:

ErrorException: Notice: Trying to get property of non-object

I'm probably just overlooking something, but I've been looking at it for quite some time and I just don't see it...

Thanks in advance for any help!

The second appointment in the collection does not have a room, thus, $appointment->room is null, so You are essentially doing null->name

enter image description here

Solution is to check if the room is set.

@if($appointment->room)
    {{ $appointment->room->name }}
@endif

Hope that helps