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
- Route [password.request] not defined
- How to disable timestamps in laravel
- Return redirect laravel not working
- Laravel 10 Breeze Authentication Example
- How to prevent host header attack in Laravel
- The openssl extension is required for SSL/TLS protection but is not available
- Get latest record by created at in Laravel
- How to get all route list
- Pass value from controller to model in laravel
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- Laravel pagination links with query string
- How to get last year records count with month wise in Laravel
- How to print form data in laravel
- How to upload local Laravel project to server ?
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- How to call model in blade laravel
- Multiple Level eager loading in Laravel
- How to insert ckeditor data into database in Laravel?
- How to get the random value form a specific column in laravel ?
- Laravel specific table Migration
- How to add dynamic page title in Laravel view
- Send id with route Laravel
- Get count of filter data, while return a small set of records
- Method Illuminate\Events\Dispatcher::fire does not exist
- Input file with max size validation in laravel