Artisan commands in Laravel: creating and running seeders
Today I was studying how to use Artisan commands in Laravel, mainly to create and run seeders.
I was building a simple wallet project and needed to understand how to populate the database with some initial data, without having to enter everything manually through the application or directly in the database.
At first I thought about inserting the data manually in the database, using some SQL client. But then I started to understand that Laravel already had a more organized way to do that using seeders.
I found it interesting how Artisan makes common development tasks easier. Instead of creating files manually or repeating commands in the database, I could use Laravel's own commands to generate structures and automate parts of the process.
In the wallet project, I could create a seeder to register some initial transaction types, like income and expense.
To create the seeder:
php artisan make:seeder TransactionTypeSeeder
Inside the seeder, I could have something like this:
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class TransactionTypeSeeder extends Seeder
{
public function run()
{
DB::table('transaction_types')->insert([
[
'name' => 'Income',
'description' => 'Money added to the wallet',
],
[
'name' => 'Expense',
'description' => 'Money removed from the wallet',
],
]);
}
}
Then, to run the seeders:
php artisan db:seed
Or to run a specific seeder:
php artisan db:seed --class=TransactionTypeSeeder
My initial impression is that Artisan commands help a lot with productivity in Laravel. In the case of seeders, I found them useful because they make the project easier to test and reproduce, mainly when I need to create initial data for an application like a wallet.