How To Update Database Records in Laravel Eloquent

In a previous section of this series, you updated an existing Artisan command in order to support the new lists feature. Update Database Records in Laravel Eloquent to insert and delete links, the demo application currently doesn’t have a command to edit existing links. This can be useful to move links between lists, for instance, or update a link description.

Laravel includes Eloquent, an Rollback Update Query in Redshift that makes it enjoyable to interact with your database. When using Eloquent, each database table has a corresponding “Model” that is used to interact with that table. In addition to retrieving records from the database table, Eloquent models allow you to insert, update, and delete records from the table as well.

If you are using Update Database Records in Laravel Eloquent in your web project then there is always possibility that you need to insert, update, and delete records instead of just fetching and displaying records. In this tutorial, I show how you can insert, update, and delete a record from MySQL database table in Laravel.

We can update the records using the DB facade with update method. To update the data in mysql table UPDATE statement is used. In this example i will learn about how to update a record or data from MySQL database using laravel framework PHP.

Laravel Update Query – Update Data From Database Using Laravel Framework

From your Update Database Records in Laravel Eloquent, first make sure you’re in your project’s root directory, then run the following to bootstrap a new Artisan command:

  • docker-compose exec app php artisan make:command LinkUpdate

This will create a new LinkUpdate.php file located at app/Console/Commands. Open the file in your code editor of choice:

  • app/Console/Commands/LinkUpdate.php

This file contains boilerplate code for a new Artisan command. You’ll update it to handle editing a link provided its unique id. This is what your handle() method needs to do:

  • Obtain an id provided by the user and check for the existence of a link with a matching id in the database.
  • If a valid link cannot be found, show an error message and exit.
  • If a valid link is found, prompt the user to provide updated values for the link description and link list.
  • Ask the user to confirm changes.
  • When confirmed, update the item in the database.

1. Table structure

I am using users table in the example.

  • CREATE TABLE `users` (
  • `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  • `username` varchar(80) NOT NULL,
  • `name` varchar(80) NOT NULL,
  • `email` varchar(80) NOT NULL
  • ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2. Database Configuration

Open .env file.

Update Database Records in Laravel Eloquent

Specify the host, database name, username, and password.

  • DB_CONNECTION=mysql
  • DB_HOST=127.0.0.1
  • DB_PORT=3306
  • DB_DATABASE=tutorial
  • DB_USERNAME=root
  • DB_PASSWORD=

3. Model

Create Model –

  • Open Command Prompt.
  • Navigate to your project folder.
  • Execute php artisan make:model Page. Here, I created Page model.
  • This will create a new file Page.php in app/ folder.
  • Create Model in Laravel 7

Create 4 methods –

  • getuserData() – Fetch all records from users table and return it. If $id is not 0 then select record by id.
  • insertData() – From this function insert new record. Update Database Records in Laravel Eloquent is exists or not in users table. If no record fetched then execute DB::table(‘users’)->insert($data); and return 1 otherwise 0.
  • updateData() – From this function update record. Execute DB::table(‘users’)->where(‘id’,$id)->update($data);.
  • deleteData() – From this function delete record. Delete record from users table according to $id.

Create your methods in the Model for Database manipulation and handle them from the controller. Make sure to add csrf_field() in your <form> otherwise Request object unable to read submit values.

Leave a Reply

Your email address will not be published. Required fields are marked *