close

[Solved] Trying to get property of a non-object problem in laravel

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Trying to get property of a non-object problem in laravel in php. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Trying to get property of a non-object problem in laravel Error Occurs?

Today I get the following error Trying to get property of a non-object problem in laravel in php.

How To Solve Trying to get property of a non-object problem in laravel Error ?

  1. How To Solve Trying to get property of a non-object problem in laravel Error ?

    To Solve Trying to get property of a non-object problem in laravel Error Worked for me:
    {{ !empty($user->role) ? $user->role->name:'' }}

  2. Trying to get property of a non-object problem in laravel

    To Solve Trying to get property of a non-object problem in laravel Error Worked for me:
    {{ !empty($user->role) ? $user->role->name:'' }}

Solution 1

Worked for me:

{{ !empty($user->role) ? $user->role->name:'' }}

Solution 2

Laravel optional() Helper is comes to solve this problem. Try this helper so that if any key have not value then it not return error

foreach ($sample_arr as $key => $value) {        
      $sample_data[] = array(       
        'client_phone' =>optional($users)->phone        
      );
    }
 print_r($sample_data);

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment