skip to Main Content

I have a weird problem with the syntax of laravel (version 7) {{ }}, where I use PHP syntax like that:

<?php echo $thread->lastReply->user->name; ?>

I get my user displayed but with the blade syntax :

{{$thread->lastReply->user->name}}

I got this error:

ErrorException
Trying to get property ‘user’ of non-object

where could the problem come from?

edited:
the method lastReplay in thread is like this:

         public function getLastReplyAttribute()
            {
                $lastReplay = $this->replies->last();
                dd($lastReplay);
                return $lastReplay;
            }

and with dump :

                ModelsReply {#3012 ▼
              #table: "ww_replies"
              #connection: "mysql"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:10 [▼
                "id" => "42"
                "thread_id" => "45"
                "user_id" => "1"
                "title" => null
                "body" => "Quia minima velit illo aut vitae eum et. Fuga consequatur atque earum fuga ad aut molestiae. Expedita ratione perspiciatis laborum hic quis unde. Numquam ipsam  ▶"
                "slug" => null
                "ip" => null
                "created_at" => "2020-11-23 12:24:19"
                "updated_at" => "2020-11-23 12:24:19"
                "deleted_at" => null
              ]
              #original: array:10 [▶]
              #changes: []
              #casts: []
              #classCastCache: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: []
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #fillable: []
              #guarded: array:1 [▶]
            }

4

Answers


  1. Is your query returning array or object? If you dump it out, you might find that it’s an array and all you need is an array access ([]) instead of an object access (->).

    Login or Signup to reply.
  2. it’s mean $thread->lastReply is not an object or usually it’s null. to handle you can use condition example:

    {{$thread->lastReply ? $thread->lastReply->user->name : 'no last reply user'}}
    

    or

    {{$thread->lastReply->user->name ?? 'no last reply user'}}
    
    Login or Signup to reply.
  3. you problem come from ->lastReply. Maybe some users dont have a lastReply ?

    Try the following:

       {{  optional($thread->lastReply, function ($lastReply)
                {
                    return $lastReply->user->name;
                });
        }}
    
    Login or Signup to reply.
  4. ErrorException
    Trying to get property 'user' of non-object
    

    This error mean either lastReply is returning null or its array

    {{$thread->lastReply->user->name}}
    

    In this syntax you need to check some data before adding relationships

    1. dump if lastReply is returning data

      {{dd($thread->lastReply)}}

    2. user should exists in model and have right foreign key in relationship.Also it should not be null that you can also check by following code

      {{dd($thread->lastReply->user)}}

    If both dumps returning data then it means lastReply or user is returning array. So you need to use array [] syntax to get data.
    You can also use {!! !!} to print data to escape content

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search