Update record after find method in lavavel
Update record after find method in lavavel
We’ll attempt to use programming in this lesson to solve the "Update record after find method in lavavel" puzzle.
You can update record after find method by passing the primary key of record to find() method and run update() method on record.-
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
- Comment .env file in laravel
- How to get path from current URL in Laravel
- The use statement with non-compound name 'Auth' has no effect
- How to get data from two tables in laravel
- How to increment column value of table in Laravel
- How to create project_user pivot table in laravel
- How to access the nth object from Laravel collection object ?
- Command to create MySQL Docker image and access the MySQL command-line interface (CLI) within a running Docker container
- 419 page expired error in Laravel
- Page loader in laravel
- Insert dummy data in users table Laravel
- Validation for multiple forms on same page in laravel
- Order by multiple columns in Laravel
- How to use bootstrap pagination in laravel 8
- Insert current date time in a column using Laravel
- How to create belongstomany relation using custom name on custom pivot table
- How to authenticate admin users in Laravel ?
- External link not working in laravel blade
- Display first n record from collection in laravel view
- Display data in table using foreach in Laravel
- How to get specific columns using with method in laravel Eloquent relationship
- Trying to access array offset on value of type null error in laravel
- SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id'
- Call to a member function update() on null
- How to upload image in laravel 8