Skip to content
Laravel Zero — home
Database

Documentation

Database

Eloquent ORM, migrations, factories, and seeders in your console application

On this page

Introduction#

The database component brings Laravel's database layer — the query builder, Eloquent, migrations, factories, and seeders — to your console application. It works exactly as it does in Laravel.

Installation#

You may install the database component using the app:install Artisan command:

bash
php application app:install database

In addition to requiring illuminate/database and fakerphp/faker, the installer will scaffold a database directory containing an empty SQLite database, a migrations directory, a factories directory, and a seeders directory. A config/database.php configuration file will also be created, with the sqlite connection configured as the default.

Running Queries#

Once the component is installed, you may run queries using the DB facade:

php
use Illuminate\Support\Facades\DB;

DB::table('movies')->insert([
    'title' => 'The Empire Strikes Back',
]);

$movies = DB::table('movies')->get();

Eloquent#

Eloquent models work like a breeze in a Laravel Zero environment. Models are typically placed within your application's app/Models directory, and may be generated using the make:model Artisan command:

bash
php application make:model Movie
php
use App\Models\Movie;

$movie = Movie::create([
    'title' => 'The Empire Strikes Back',
]);

$movies = Movie::where('year', '>', 1980)->get();

Migrations, Factories, and Seeders#

Laravel's migrations, factories, and seeders are all included, along with the Artisan commands you would expect:

bash
php application make:migration create_movies_table

php application migrate

php application make:factory MovieFactory

php application make:seeder MovieSeeder

php application db:seed

Redis#

Laravel Zero also provides a component for Redis, an in-memory data structure store. To get started, install the redis component:

bash
php application app:install redis

Once the component is installed, configure your Redis connections within your config/database.php configuration file, and you may use the Redis facade:

php
use Illuminate\Support\Facades\Redis;

Redis::set('name', 'Daniel LaRusso');

$name = Redis::get('name');

Consult the Redis documentation on the Laravel website for full details.

Building Your Application#

Including the Database Directory#

The database directory is not included in a PHAR build by default. If your application relies on migrations, factories, or seeders at runtime, add the directory to the directories section of your box.json file:

json
"directories": [
    "app",
    "bootstrap",
    "config",
    "database",
    "vendor"
],

Choosing a Database Location#

Files inside a PHAR archive are read-only, so a SQLite database cannot live within your build. By default, the sqlite connection points at database_path('database.sqlite'), which resolves to a path inside the archive.

Instead, you should store the database somewhere on the user's machine. A "dot" directory inside the user's home directory is a good choice:

diff
// config/database.php...

'connections' => [
    'sqlite' => [
        'driver' => 'sqlite',
        'url' => env('DB_URL'),
-        'database' => database_path('database.sqlite'),
+        'database' => $_SERVER['HOME'].'/.movie-cli/database.sqlite',
        'prefix' => '',
        'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
    ],
],

Warning: This file will not exist when a user first installs your application. You should either create and migrate it on demand, or provide an install command that does so.

Spotted a mistake? Edit this page on GitHub.