Error : Trying To Get Property Of ID Non-Object All Possible Solution Guideline In Laravel - PHP

  • 30-06-2022
  • 768
  • Laravel 9
  • Haresh Chauhan

Trying to get property of non-object, this error developer-facing many time while they developing laravel application, This error comes with many different forms it depends on your attributes and coding.

There are many reasons for this error arriving while you developing your site.

The Major Reason For This Error

  • Used array as an object so it will throw the none-object error.
  • While you are accessing the null variable using the pointer sign.
  • Trying to access laravel's eloquent relationship.
  • The query returing array and accessing as object.

is your query returning an array or object? If you dump it and check it out You might find an array of output, not an object, You need to access the array using ([]) and object access using (->).

Example & Understanding

If you are accessing Array:

echo $array['key_name'];

If you are accessing Object:

echo $object->key_name;

If you are accessing Null Variable:

$a = null;

if(isset($a->key_name)){
    // true
}

Development Example & Understanding

In this example, I am finding the user record from its id using the find method and accessing the name of the user from the $user variable. If the user id is wrong then the variable will have null so it will throw an error Trying to get property of name non-object because of the name on set in this variable.

You can see in codepanel How it solved and fix the error. using firstOrFail and optional method. This will prevent the error.

$user = User::find($id);

$user->name // error throws : Trying to get property of name non-object

// SOLUTION 1

$user // make sure your $user not null

$user->name // make sure NAME KEY inside the ($user) variable isset.

// SOLUTION 2

$user = User::findOrFail($id);

// SOLUTION 3

$user = User::find($id);

optional($user)->name

Make sure you are not using an array as an object. You must need to access array using ([]) and object (->) notation.

You can see in an example how to use and fix an error.

// Is Array Not Object
$user = [
    'name' => 'Haresh',
    'age' => 23,
];

// Can not Use as Object

$user->name // error throws : Trying to get property of name non-object
$user->age // error throws : Trying to get property of age non-object

// SOLUTION 1

echo $user['name'];
echo $user['age'];

// SOLUTION 2

$user = (object) [ // convert array to object.
    'name' => 'Haresh',
    'age' => 23,
];

$user->name 

You might reason of eloquent relationship affect follow an example.

public function users($id)
{
    $article = User::with('posts')->where('id', $id)->firstOrFail();

    $article->posts->category;

    // REPLACE TO 

    $article->posts['category'];
}

Blade File Example Looping

If the user have any role then it will show else nothing will show.

So it don'ts have a role and if you are forced to print the role it will give you this error. So check with empty() this means role not empty it will not show else role will printed using.

<table>
    <tr>
        <th>Name</th>
        <th>Role</th>
    </tr>
    @foreach ($users as $user)
    <tr>
        <td>{{ $user->name }}</td>
        <td>{{ !empty($user->role) ? $user->role->name:'' }}</td>
    </tr>
    @endforeach
</table>

Laravel Also provides optional() helper function that will check if the object can be accessible then will return else return null. So this helper function will prevent this error.

<table>
    <tr>
        <th>Name</th>
        <th>Role</th>
    </tr>
    @foreach ($users as $user)
    <tr>
        <td>{{ $user->name }}</td>
        <td>{{ optional($user->role)->name }}</td>
    </tr>
    @endforeach
</table>

If you are using PHP 8, you can use null safe operator (?)

<table>
    <tr>
        <th>Name</th>
        <th>Role</th>
    </tr>
    @foreach ($users as $user)
    <tr>
        <td>{{ $user?->name }}</td>
        <td>{{ $user?->role?->name }}</td>
    </tr>
    @endforeach
</table>

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :