Exception Trying to get property of non-object (View: ) only gets thrown for admin

kyle-hurst

Sep 18th, 2017 02:06 PM

For some reason when a user has the out of the box role of admin I can't use a hasOne relationship without getting this error in my view:

ErrorException in 4d5566fef805f9eb212e44abf59149927b7529a4.php line 16: Trying to get property of non-object (View: /Applications/MAMP/htdocs/mysite/resources/views/conversations/index.blade.php)

When I use just a Normal User role that's out of the box with Voyager the following code works absolutely fine:

ConversationsController

public function index()
{
    

    $conversations = Auth::user()->conversations()->with('latestMessage')->orderBy('updated_at','desc')->take('20')->get();
     
    //dd($conversations);

    //return $conversations;

    return view('conversations.index', compact('conversations'));
}

Conversation Model

//Relationship that gets the latest message for a conversation
    public function latestMessage()
{
		return $this->hasOne('App\Message')->latest();
}

conversations.index

<div class="panel-body">
   @foreach ($conversations as $conversation)
        <p><a href="{{url('conversation/'. $conversation->id ) }}">{{ $conversation->name }}</a></p>

<!-- If I take from here -->
        <img style="width:32px; height:32px; border-radius:50%" src="{{$conversation->latestMessage->sender->avatar}}">

        {{$conversation->latestMessage->body}}

        <p class="pull-right">{{ $conversation->latestMessage->created_at->diffForHumans() }}</p>

<!-- To here out -->
        <hr>
    @endforeach
</div>

If I take the block of code where the html comments are the index runs fine for the admin. It's just those bits of code. I can even swap the latestMessage for the has many messages to get a list of messages for each conversation and it works perfectly fine for admin.

<div class="panel-body">
   @foreach ($conversations as $conversation)
        <p><a href="{{url('conversation/'. $conversation->id ) }}">{{ $conversation->name }}</a></p>

<!--  hasMany messages instead of hasOne latestMessage -->
        @foreach($conversation->messages as $message)
            {{$message->body}}

            {{ $message->sender->name }}
        @endforeach

<!-- end note -->
        <hr>
    @endforeach
</div>

If I return the conversation variable in the ConversationsController, I get what looks to be the samething:

Admin Output

[
    {
        "id": 5,
        "name": "Conversation between Jamie and Admin",
        "is_private": 1,
        "created_at": "2017-09-02 02:35:22",
        "updated_at": "2017-09-02 02:35:22",
        "pivot": {
            "user_id": 1,
            "conversation_id": 5
        },
        "latest_message": {
            "id": 48,
            "body": "Another message",
            "conversation_id": 5,
            "sender_id": 1,
            "type": "user_message",
            "created_at": "2017-09-02 02:39:36",
            "updated_at": "2017-09-02 02:39:36"
        }
    }
]

Non admin output

[
    {
        "id": 6,
        "name": "Conversation between Corbin and JaneDoe",
        "is_private": 1,
        "created_at": "2017-09-18 04:02:00",
        "updated_at": "2017-09-18 04:02:48",
        "pivot": {
            "user_id": 4,
            "conversation_id": 6
        },
        "latest_message": {
            "id": 88,
            "body": "Hi",
            "conversation_id": 6,
            "sender_id": 8,
            "type": "user_message",
            "created_at": "2017-09-18 04:02:48",
            "updated_at": "2017-09-18 04:02:48"
        }
    }
]

I haven't done anything funky to conversation permissions, or anything like that. So I really have no clue what's going on. Everything works fine for Normal Users. I haven't screwed around with role permissions, or anything, they're just stock used out of the box. I just can't seem to get around that error with a user with an admin role.

mark

Sep 18th, 2017 05:02 PM

Can you open the cached view file 4d5566fef805f9eb212e44abf59149927b7529a4.php and find line 16.

kyle-hurst

Sep 18th, 2017 07:11 PM

Under storage/framework/views right?

<!-- Line 16 -->
<img style="width:32px; height:32px; border-radius:50%" src="<?php echo e($conversation->latestMessage->sender->avatar); ?>">

<?php echo e($conversation->latestMessage->body); ?>

<p class="pull-right"><?php echo e($conversation->latestMessage->created_at->diffForHumans()); ?></p>

<!-- Line 24 -->
mark

Sep 18th, 2017 07:47 PM

latestMessage does not have a sender->avatar. Can you try do this:

$conversations = Auth::user()->conversations()->with('latestMessage', 'latestMessage.sender')->orderBy('updated_at','desc')->take('20')->get();
kyle-hurst

Sep 18th, 2017 08:29 PM

I tried that and got the same issue. Thanks for your help so far though.

mark

Sep 19th, 2017 06:25 AM

What do you get if you output it as JSON? Just to check if we get the right properties.