
Call to a member function update() on null
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; }
0Output :
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.
Random Code Snippet Queries: Laravel
- Laravel get single row by id
- Calculate age from date of birth in Laravel
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- How to add active class to menu item in laravel
- Post model with title and body in laravel 8
- Ignore Records where a field has NULL value in Laravel
- Get last week data in Laravel
- How to get route method name in Laravel
- Method Illuminate\Events\Dispatcher::fire does not exist
- PhpMyAdmin - Error The mysqli extension is missing
- Create project factory and seed data in laravel
- How to get user information using hootlex/laravel-friendships package in laravel
- Pagination in laravel
- How to restore deleted records in laravel
- How to pass link from controller to view in laravel on ajax call
- How to create belongstomany relation using custom name on custom pivot table
- Seed database using SQL file in Laravel
- How to generate .env file for laravel?
- Get the post details if it has at least one comment in comments table
- 419 page expired error in Laravel
- Send OTP using textlocal api in laravel
- How to use or operator in laravel
- How to add script on specific view file in laravel while extending layout
- Class 'Facade\Ignition\IgnitionServiceProvider' not found
- Remove public from url in laravel project