Laravel Eloquent ORM: Secure Deletion by Managing Related Data
💡 What You Will Learn in This Guide
In this guide, you will learn how to securely delete records using the Laravel Eloquent ORM's delete() method. Additionally, we will show you how to apply a bulk update and how to enable the Soft Delete method so that when deleting a main record (e.g. List), the sub-records connected to it (e.g. Link) are not lost.
⚙️ 1. Creating a New Artisan Command
First, let's create a custom delete command.
docker-compose exec app php artisan make:command ListDelete
This command creates the file
app/Console/Commands/ListDelete.php.
🧩 2. Implementing Delete Logic
Open ListDelete.php and edit it to handle the deletion:
<?php
namespace App\Console\Commands;
use App\Models\Link;
use App\Models\LinkList;
use Illuminate\Console\Command;
class ListDelete extends Command
{
protected $signature = 'list:delete {list_slug}';
protected $description = 'Listeleri Siler ve Bağlantıları Varsayılana Atar';
public function handle()
{
$list_slug = $this->argument('list_slug');
$list = LinkList::firstWhere('slug', $list_slug);
if ($list === null) {
$this->error("Geçersiz veya var olmayan Liste.");
return 1;
}
if ($this->confirm("'$list->title' listesini silmeyi onaylıyor musunuz? Bağlantılar varsayılana atanacak.")) {
$default_list = LinkList::firstWhere('slug', 'default');
// Varsayılan liste yoksa oluştur
if (!$default_list) {
$default_list = new LinkList();
$default_list->title = 'default';
$default_list->slug = 'default';
$default_list->save();
}
$this->info("Bağlantılar varsayılan listeye atanıyor...");
// Toplu güncelleme işlemi
Link::where('link_list_id', $list->id)
->update(['link_list_id' => $default_list->id]);
// Listeyi sil
$list->delete();
$this->info("Liste Başarıyla Silindi.");
}
return 0;
}
}
This code finds the list to delete, moves the associated links to the default list, and then permanently deletes the list.
🧠 3. Understanding Process Flow
firstWhere()→ Finds the list according toslugentered by the user.confirm()→ Asks for confirmation from the user.update()→ Moves associated links to the default list.delete()→ Clears the list via Eloquent Model.
All these operations work with a single command.
🧰 4. Testing the Command
To check existing connections:
docker-compose exec app php artisan link:show
To delete a list:
docker-compose exec app php artisan list:delete digitalocean
Terminal output:
Confirm deleting the list 'digitalocean'? Links will be reassigned to the default list. (yes/no) [no]:
> y
Reassigning links to default list...
Liste Başarıyla Silindi.
In the last case, your old “digitalocean” connections will be transferred to the default list.
🔄 5. Enabling Soft Delete Method
If you do not want deleted records to be completely removed from the database, you can use Soft Delete.
In your model file (LinkList.php) add the following property:
use Illuminate\Database\Eloquent\SoftDeletes;
class LinkList extends Model
{
use SoftDeletes;
}
Then add the following line to your migration file:
$table->softDeletes();
Run Migration:
php artisan migrate
In this way, the
deleted_atcolumn is filled in instead of completely deleting the records.
To also query deleted records:
LinkList::withTrashed()->get();
If you want to delete it permanently:
$list->forceDelete();
⚡ 6. Bulk Delete and Update Alternatives
If you want to delete all records at once:
LinkList::truncate(); // tabloyu tamamen sıfırlar
If you want to delete based on certain condition:
Link::where('link_list_id', 5)->delete();
For bulk update:
Link::where('status', 'inactive')->update(['status' => 'archived']);
These operations do not trigger model events and provide higher performance.
❓ Frequently Asked Questions (FAQ)
1. What is the difference between delete() and forceDelete()?
If delete() soft delete is active, it only updates the deleted_at field. forceDelete() deletes the recording completely.
2. Why is bulk updating preferred?
Changing hundreds of records in a single query provides performance. It does not require a loop (foreach).
3. How does Soft Delete hide data?
Laravel adds the deleted_at IS NULL condition in default queries. Deleted records are hidden but not lost.
4. What does confirm() do?
Gets interactive confirmation from the user on the command line.
5. Is it possible to restore all records?
Yes. You can restore Soft Delete records with the LinkList::onlyTrashed()->restore(); command.
🏁 Result
You can now perform secure deletions while preserving associated data with Laravel Eloquent ORM. You can maintain database integrity and prevent data loss with bulk updates, Soft Delete, and special Artisan commands.
💡 Create a virtual server on GenixNode now to optimize and securely manage your Laravel projects! ☁️

