Database Management Guide with Laravel Artisan Commands
💡 What You Will Learn in This Guide
In this guide, you will learn how to manage database records using Laravel's built-in command line tool Artisan. By performing add, list and delete operations via CLI (command line), you will manage your application safely and quickly without a web interface.
🧠 Technical Summary
- link:new → Adding a new record
- link:list → List records in table format
- link:delete → Delete registration (by ID)
All operations will be done using Eloquent ORM.
🔧 Step 1: Add New Record Command (link:new)
🛠 Generating Command
php artisan make:command LinkNew
This command creates the file
LinkNew.phpin theapp/Console/Commandsdirectory.
✏️ Editing Script
nano app/Console/Commands/LinkNew.php
💬 Receiving and Validating Input
$url = $this->ask('Bağlantı URL\'si:');
if (!filter_var($url, FILTER_VALIDATE_URL)) {
$this->error("Geçersiz URL. İşlem iptal edildi.");
return 1;
}
$description = $this->ask('Bağlantı açıklaması:');
$this->ask()receives input from the user, verification is done withfilter_var.
💾 Saving to Database
if ($this->confirm('Bu bilgiler doğru mu?')) {
$link = new Link();
$link->url = $url;
$link->description = $description;
$link->save();
$this->info("Bağlantı başarıyla kaydedildi.");
}
return 0;
▶️ Running the Command
php artisan link:new
The command asks you for connection information and saves it in the database.
📋 Step 2: Record Listing Command (link:list)
🛠 Generating Command
php artisan make:command LinkList
✏️ Editing Script
public function handle()
{
$headers = ['ID', 'URL', 'Açıklama'];
$links = Link::all(['id', 'url', 'description'])->toArray();
$this->table($headers, $links);
return 0;
}
$this->table()Shows CLI output in tabular format.
▶️ Running the Command
php artisan link:list
Lists records in tabular format.
❌ Step 3: Delete Command (link:delete)
🛠 Generating Command
php artisan make:command LinkDelete
✏️ Mandatory Argument Definition
protected $signature = 'link:delete {link_id}';
The
{link_id}parameter specifies the ID of the record to be deleted.
💣 Applying Delete
public function handle()
{
$link_id = $this->argument('link_id');
$link = Link::find($link_id);
if ($link === null) {
$this->error("Geçersiz veya mevcut olmayan bağlantı ID'si.");
return 1;
}
if ($this->confirm("{$link->url} bağlantısını silmek istediğinize emin misiniz?")) {
$link->delete();
$this->info("Bağlantı silindi.");
}
return 0;
}
▶️ Running the Command
php artisan link:delete 3
Deletes the record with ID 3.
❓ Frequently Asked Questions (FAQ)
1. Why are artisan commands preferred?
CLI-based operations are more secure because only users with access to the server can run them.
2. What is the difference between Argument ({link_id}) and Option (--option)?
Arguments are mandatory values, while options are optional and change the behavior of the command.
3. My command does not appear in the php artisan list, why? If the command is not added to the $commands array in the App\Console\Kernel.php file, it may not appear in the list.
4. Why are Artisan scripts safer than web forms?
Because it requires SSH access; It is not accessible to external browsers.
5. Can it also be used in a Docker environment?
Yes.
docker-compose exec app php artisan link:list
🏁 Result
Artisan commands make managing your Laravel application easy and secure through the terminal. With this method, you can manage database records professionally without a web interface.
Try Artisan commands by testing your Laravel application on the GenixNode platform now! 🚀

