Lỗi trying to get property danhmuc1 of non-object năm 2024

$userUpdate will return the results as a collection. You need to loop that. to get just the first result you can use first() method

$userUpdate->first()->name;

if the collection is empty, you will again get the same error, to fix that use:

$userUpdate->first() ?: $userUpdate->first()->name;

Last updated 7 years ago.

Thank you for your response I just fixed it using the query builder

$userInfo = DB::table('users')->where('id', $id)->first();
return view('users.update', ['userInfo' => $userInfo]);

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

If you are a Laravel developer, you have probably encountered the “Trying to get property of non-object” error at some point in your coding journey. This error is quite common and can be frustrating to deal with, especially when you are not sure where to start.

In this article, we will discuss the “Trying to get property of non-object” error in Laravel, its causes, and how to fix it.

What Causes the “Trying to get property of non-object” Error in Laravel?

The “Trying to get property of non-object” error occurs when you attempt to access a property of an object that doesn’t exist or has not been set. In other words, you are trying to access a property of a null object, which will result in an error.

This error is commonly caused by a typo in the object’s property name or a failure to properly initialize the object.

How to Fix the “Trying to get property of non-object” Error in Laravel?

Fixing the “Trying to get property of non-object” error in Laravel can be straightforward once you understand what’s causing it. Here are some steps to help you resolve the error:

Step 1: Use Var_dump or DD to Inspect the Object

The first step is to inspect the object and check its type. You can use the var_dump or dd function to do this.

For example:

dd($object);

If the variable is not an object, then you need to find out why it is not being set correctly. Check if the variable name is correctly spelled, or if the object has been properly initialized.

Step 2: Check if the Property Exists

If the variable is an object, the next step is to check if the property you are trying to access exists on the object. If the property does not exist, you will need to add it to the object or modify your code to access a different property.

For example:

php if (isset($object->property)) { // Access the property here } else { // Property does not exist, handle the error } Step 3: Use Null Coalescing Operator

If you are sure that the object and the property exist, but you are still getting the “Trying to get property of non-object” error, you can use the null coalescing operator to check if the object property is null.

For example:

$property = $object->property ?? null; This code will assign null to the $property variable if the $object->property is null.

Conclusion

The “Trying to get property of non-object” error in Laravel can be frustrating, but it can be fixed by following the steps we have discussed. Always remember to inspect the object, check if the property exists, and use the null coalescing operator to avoid errors in the future.

By following these steps, you should be able to fix the “Trying to get property of non-object” error and write better, more reliable code.