Note: The database feature is disabled by default in Laravel Console Starter to keep the framework lightweight for console-focused applications.
This guide explains how to enable database functionality when your console application needs to interact with databases.
While console applications may not frequently use local databases, it's common to utilize remote databases like AWS RDS when running them in GitHub Actions. This guide covers:
- Adding database files from the official Laravel repository
- Updating Composer autoload configuration
- Configuring database connections with repository secrets
You need to download the following files and directories from the official Laravel repository:
Download the entire database/ directory structure:
database/
├── factories/
│ └── UserFactory.php
├── migrations/
│ ├── 0001_01_01_000000_create_users_table.php
│ ├── 0001_01_01_000001_create_cache_table.php
│ └── 0001_01_01_000002_create_jobs_table.php
└── seeders/
└── DatabaseSeeder.php
Create the directory structure in your project:
mkdir -p database/factories database/migrations database/seedersDownload and place files:
database/factories/UserFactory.phpdatabase/migrations/*(all migration files)database/seeders/DatabaseSeeder.php
Download config/database.php from the Laravel repository and place it in your config/ directory:
# Download to your project
curl -o config/database.php https://raw.githubusercontent.com/laravel/laravel/12.x/config/database.phpDownload app/Models/User.php from the Laravel repository:
# Create Models directory if it doesn't exist
mkdir -p app/Models
# Download User model
curl -o app/Models/User.php https://raw.githubusercontent.com/laravel/laravel/12.x/app/Models/User.phpEdit your composer.json file to add autoload paths for database factories and seeders:
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
}
}After updating composer.json, regenerate the autoload files:
composer dump-autoloadUpdate your .env file with database credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_passwordFor SQLite (simpler for local development):
DB_CONNECTION=sqlite
# DB_DATABASE=database/database.sqliteIf DB_DATABASE is not set, database/database.sqlite will be used.
When using databases with GitHub Actions, configure database credentials using repository secrets for security.
- Go to your GitHub repository
- Navigate to Settings > Secrets and variables > Actions
- Click New repository secret
- Add the following secrets:
APP_KEY- Your Laravel application keyDB_HOST- Database host addressDB_DATABASE- Database nameDB_USERNAME- Database usernameDB_PASSWORD- Database password
Update your .github/workflows/cron.yml or create a new workflow with database configuration:
name: cron
on:
schedule:
- cron: '0 0 * * *' # Daily at midnight UTC
jobs:
cron:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.4
extensions: pdo, pdo_mysql
- name: Install Dependencies
run: composer install --no-dev -q
- name: Copy Environment File
run: cp .env.example .env
- name: Generate Application Key
run: php artisan key:generate
- name: Run Migrations
run: php artisan migrate --force
env:
APP_KEY: ${{ secrets.APP_KEY }}
DB_HOST: ${{ secrets.DB_HOST }}
DB_DATABASE: ${{ secrets.DB_DATABASE }}
DB_USERNAME: ${{ secrets.DB_USERNAME }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
- name: Run Command
run: php artisan your:command
env:
APP_KEY: ${{ secrets.APP_KEY }}
DB_HOST: ${{ secrets.DB_HOST }}
DB_DATABASE: ${{ secrets.DB_DATABASE }}
DB_USERNAME: ${{ secrets.DB_USERNAME }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}Once database configuration is complete, run migrations to create tables:
php artisan migrateFor fresh installations with seeders:
php artisan migrate:fresh --seedAfter enabling database functionality, you can use Eloquent models and database queries in your console commands:
<?php
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class ProcessUsers extends Command
{
protected $signature = 'users:process';
protected $description = 'Process user data';
public function handle(): int
{
// Using Eloquent
$users = User::where('active', true)->get();
foreach ($users as $user) {
$this->info("Processing user: {$user->email}");
// Your processing logic here
}
// Using Query Builder
$count = DB::table('users')
->where('created_at', '>', now()->subDays(7))
->count();
$this->info("New users this week: {$count}");
return Command::SUCCESS;
}
}You can configure multiple database connections in config/database.php:
'connections' => [
'local' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
// ... local configuration
],
'production' => [
'driver' => 'mysql',
'host' => env('PROD_DB_HOST'),
// ... production configuration
],
],Use specific connections in your commands:
$users = DB::connection('production')->table('users')->get();For testing commands that use databases, Laravel provides database testing utilities:
<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ProcessUsersTest extends TestCase
{
use RefreshDatabase;
public function test_process_users_command(): void
{
// Create test data
User::factory()->count(5)->create();
// Run command
$this->artisan('users:process')
->assertExitCode(0)
->assertSuccessful();
}
}- AWS RDS MySQL/PostgreSQL
- Google Cloud SQL
- Azure Database
- PlanetScale
- Supabase
- SQLite (lightweight, file-based)
- MySQL (via Docker or local installation)
- PostgreSQL (via Docker or local installation)
- Laravel Sail (Docker development environment)
- Never commit credentials to version control
- Use repository secrets for GitHub Actions workflows
- Rotate database passwords regularly
- Use read-only connections when write access isn't needed
- Implement connection pooling for high-frequency tasks
- Enable SSL/TLS for remote database connections
# Check migration status
php artisan migrate:status
# Rollback last migration
php artisan migrate:rollback
# Reset all migrations
php artisan migrate:reset# Test database connection
php artisan tinker
>>> DB::connection()->getPdo();- Ensure all secrets are properly configured
- Verify database host is accessible from GitHub Actions runners
- Check firewall rules allow connections from GitHub Actions IP ranges
- Enable SSL if required by your database provider