
Call to a member function update() on null
Call to a member function update() on null
Hello everyone, in this post we will examine how to solve the "Call to a member function update() on null" programming puzzle.
Sometimes you get the error Call to a member function update() on null when you try to update the record using find() method which does not exist in the table or you pass the argument with value null to find() method-
Update record using find() method with static value in laravel 8
//routes\web.php use App\Http\Controllers\HomeController; Route::get('/update-record',[HomeController::class, 'updateRecord']); //app\Http\Controllers\HomeController.php //Import post model in controller use App\Models\Post; //Controller's method to update record with static value public function updateRecord(){ $id = 1; $post = Post::find($id)->update(['description'=> 'Lorem ipsum']); echo $post; }
Output :
If record exists with specific argument value and update query executed successfully
1
If record does not exists and update query not executed successfully
Error : Call to a member function update() on null
The error Call to a member function update() on null means that Post::find($id) returned null. This may be because $id is itself null, or the value of $id does not exist in the id column of the posts table.
You have to check the $id value should not be null and after that you have to check the record with this id (value) exists in the table. It will return 1 if everything is ok else it will return Call to a member function update() on null.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Route not defined in Laravel
- Get last year created records in Laravel
- External link not working in laravel blade
- Display data in table using foreach in Laravel
- Get id of last inserted record in laravel
- Conditional validation in laravel
- There are no commands defined in the "route:" namespace
- Get last week data in Laravel
- Call to a member function pluck() on array
- Store logged in user details in session and display in view in laravel
- Ignore Records where a field has NULL value in Laravel
- How to customize or Change validation error messages
- In order to use the Auth::routes() method, please install the laravel/ui package
- Class App\Http\Controllers\Admin\UserController Does Not Exist
- How to display validation error in laravel
- Pass value from controller to model in laravel
- Create project table with model and migration
- How to remove P tag from CkEditor in Laravel?
- Update if exist else insert new record in laravel
- Delete all related comments on deleting a post in Laravel
- After image selected get validation error in laravel
- How to add script on specific view file in laravel while extending layout
- Permission denied error while creating storage link in Laravel
- Use withCount() to Calculate Child Relationship Records
- How to create pivot table in laravel using migration