Support Online
Skip to main content

How to Update Database Records with Laravel Eloquent?

Laravel Eloquent ORM is a powerful tool that simplifies database management. In this guide, you will learn how to update existing records in the database with Laravel, and also discover how you can change the description and listings of links using interaction with Artisan commands.

What Will You Learn in This Guide?

You can learn how to update an existing record in the database with Laravel Eloquent ORM. Additionally, you will learn how to update the descriptions of existing links and the associated list with data received from the user with Artisan commands (ask, confirm).

1. Starting a New Artisan Command

We will create a new Artisan command to edit the links in the database.

Creating the Script:

docker-compose exec app php artisan make:command LinkUpdate

This command automatically creates the app/Console/Commands/LinkUpdate.php file.


2. Writing the Signature and Logic of the LinkUpdate Command

We will open the LinkUpdate.php file we created and add the logic to perform the update. This logic involves updating the link description and listing with data received from the user.

Making Required Classes Available:

use App\Models\Link;
use App\Models\LinkList;

These lines provide easy access to Eloquent models.

Defining the Command Signature:

protected $signature = 'link:update {link_id}';

This line forces the command to run as link:update and requires an argument link_id.

Implementing the handle() Method

This method will find the link, ask the user questions and save the changes to the database.


public function handle()
{
$link_id = $this->argument('link_id');
$link = Link::find($link_id);

if ($link === null) {
$this->error("Geçersiz veya bulunamayan Link ID'si.");
return 1;
}

// Kullanicidan yeni bilgileri al
$link->description = $this->ask('Link Açıklaması (Mevcut kalsın)') ?? $link->description;
$list_name = $this->ask('Link Listesi (Mevcut kalsın)') ?? $link->link_list->title;

$this->info("Yeni Açıklama: $link->description");
$this->info("Yeni Liste: " . $list_name);

if ($this->confirm('Bu bilgiler doğru mu?')) {
// Liste var mı kontrol et, yoksa oluştur
$list = LinkList::firstWhere('slug', $list_name);
if (!$list) {
$list = new LinkList();
$list->title = $list_name;
$list->slug = $list_name;
$list->save();
}

// 🔑 Linkin listesini ilişkilendir ve veritabanına kaydet
$link->link_list()->associate($list)->save();
$this->info("Güncelleme Tamamlandı.");
}

return 0;
}

This code saves the data received from the user into the database and updates the list associated with the link.


3. Testing the Update Process

Let's test the command we created and move the link to another list.

First, let's see the existing links in the database:


docker-compose exec app php artisan link:show

This command shows which lists existing links belong to.

Let's move the link with ID 2 to a new list named genixnode_blog.


docker-compose exec app php artisan link:update 2

Confirm the operation by typing y in the confirmation prompt that appears on the command line and pressing ENTER.


Link Açıklaması (Mevcut kalsın):
>
Link Listesi (Mevcut kalsın):
> genixnode_blog

Yeni Açıklama: Laravel Tutorial at DigitalOcean
Yeni Liste: genixnode_blog

Bu bilgiler doğru mu? (yes/no) [no]:
> y

Güncelleme Tamamlandı.
Verifying the Result

Checking the link again, you can see that the link with ID 2 has now been moved to the genixnode_blog list:


docker-compose exec app php artisan link:show

The update process was successful and the new list of the link appears.


Frequently Asked Questions (FAQ)

  1. How can I update database records with Laravel Eloquent?

You can use the update() method to update records with Laravel Eloquent. If you are trading through the model, you can also update your record with $model->save().

  1. What does the ask() method do?

This method is used to get data from the user on the command line. It asks the user a question and returns the entered value as the answer.

  1. What does the associate() method do?

The associate() method is used to manage Laravel relationships. It establishes the relational connection by updating the relational key of the parent model to which a model belongs, (örneğin link_list_id), with the correct ID.

  1. What does this->confirm() do?

This method is used to ensure that the user confirms a transaction. It creates a security layer for irreversible operations such as deleting and updating.

  1. What does LinkList::firstWhere('slug', $list_name) do?

This expression finds the first record in the LinkList model with the slug value $list_name and uses this record for association.


Result

In this guide, you learned how to update an existing record in the database with Laravel Eloquent ORM and edit your links using user interaction with Artisan commands. Test and go live your Laravel application on the GenixNode platform now!