| title | Database |
|---|---|
| description | Eloquent ORM, migrations, factories, and seeders in your console application |
- Introduction
- Installation
- Running Queries
- Eloquent
- Migrations, Factories, and Seeders
- Redis
- Building Your Application
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.
You may install the database component using the app:install Artisan command:
php application app:install databaseIn 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.
Once the component is installed, you may run queries using the DB facade:
use Illuminate\Support\Facades\DB;
DB::table('movies')->insert([
'title' => 'The Empire Strikes Back',
]);
$movies = DB::table('movies')->get();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:
php application make:model Movieuse App\Models\Movie;
$movie = Movie::create([
'title' => 'The Empire Strikes Back',
]);
$movies = Movie::where('year', '>', 1980)->get();Laravel's migrations, factories, and seeders are all included, along with the Artisan commands you would expect:
php application make:migration create_movies_table
php application migrate
php application make:factory MovieFactory
php application make:seeder MovieSeeder
php application db:seedLaravel Zero also provides a component for Redis, an in-memory data structure store. To get started, install the redis component:
php application app:install redisOnce the component is installed, configure your Redis connections within your config/database.php configuration file, and you may use the Redis facade:
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.
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:
"directories": [
"app",
"bootstrap",
"config",
"database",
"vendor"
],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:
// 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
installcommand that does so.