
Update record after find method in lavavel
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; }
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
- How to call controller function from view in Laravel
- Check if Relationship Method Exists in Laravel
- How to get random string in Laravel
- Seed database using SQL file in Laravel
- Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
- Touch parent updated_at in Laravel
- Laravel 7 login error message not showing
- Create project factory and seed data in laravel
- How to pass two variables in HREF in laravel
- Route group with URI prefix using middleware and route name prefixes
- Array to string conversion laravel blade
- How to check if user has created any post or not in laravel
- Class 'App\Providers\Auth' not found
- Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0"
- How to Access Array in blade laravel
- How to return a column with different name in Laravel
- Use withCount() to Calculate Child Relationship Records
- How to pass variable from controller to model in Laravel
- How to add a key value pair to existing array in laravel
- How to insert multiple rows in mysql using loop in laravel?
- Laravel create default admin user
- Add class to body in laravel view
- How to show data by ID in laravel?
- Validation for multiple forms on same page in laravel
- Global scope in Laravel with example