statamic / eloquent-driver
Allows you to store Statamic data in a database.
Installs: 272 915
Dependents: 3
Suggesters: 0
Security: 0
Stars: 110
Watchers: 12
Forks: 77
Open Issues: 8
Requires
- php: ^8.1
- statamic/cms: ^5.41
Requires (Dev)
- doctrine/dbal: ^3.8
- laravel/pint: ^1.0
- orchestra/testbench: ^8.28 || ^9.6.1
- phpunit/phpunit: ^10.5.35
- dev-master
- v4.19.1
- v4.19.0
- v4.18.0
- v4.17.1
- v4.17.0
- v4.16.2
- v4.16.1
- v4.16.0
- v4.15.2
- v4.15.1
- v4.15.0
- v4.14.3
- v4.14.2
- v4.14.1
- v4.14.0
- v4.13.0
- v4.12.3
- v4.12.2
- v4.12.1
- v4.12.0
- v4.11.1
- v4.11.0
- v4.10.0
- v4.9.0
- v4.8.1
- v4.8.0
- v4.7.0
- v4.6.0
- v4.5.0
- v4.4.0
- v4.3.1
- v4.3.0
- v4.2.0
- v4.1.0
- v4.0.0
- 3.x-dev
- v3.4.1
- v3.4.0
- v3.3.3
- v3.3.2
- v3.3.1
- v3.3.0
- v3.2.0
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.10.0
- v2.9.0
- v2.8.0
- v2.7.0
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.1
- v2.5.0
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.0
- v1.2.0
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.3.0
- v0.2.0
- v0.1.2
- v0.1.1
- v0.1.0
This package is auto-updated.
Last update: 2024-12-12 06:53:56 UTC
README
Provides support for storing your Statamic data in a database, rather than flat files.
Installation & Usage
You can install and configure the Eloquent Driver using a single command:
php please install:eloquent-driver
The command will install the statamic/eloquent-driver
package, publish the config file, then prompt you to select which repositories you wish to move to the database. The command will then publish the relevant migrations and run php artisan migrate
behind the scenes.
The command will also give you the opportunity to indicate whether you'd like existing data to be imported.
Importing flat-file content
If you originally opt-out of importing existing content, then later change your mind, you can import existing content by running the relevant commands:
- Assets:
php please eloquent:import-assets
- Blueprints and Fieldsets:
php please eloquent:import-blueprints
- Collections:
php please eloquent:import-collections
- Entries:
php please eloquent:import-entries
- Forms:
php please eloquent:import-forms
- Globals:
php please eloquent:import-globals
- Navs:
php please eloquent:import-navs
- Revisions:
php please eloquent:import-revisions
- Taxonomies:
php please eloquent:import-taxonomies
- Sites:
php please eloquent:import-sites
Assets
Empty Folders
If your assets are being driven by the Eloquent driver then the database is used as the source of truth for the folder listing, so if no file is present inside a folder then it will not be shown.
Syncing
If your assets are being driven by the Eloquent Driver and you're managing your assets outside of Statamic (eg. directly in the filesystem), you should run the php please eloquent:sync-assets
command to add any missing files to the database, and remove files that no longer exist on the filesystem.
Exporting to flat files
If you wish to move back to flat-files, you may use the following commands to export your content out of the database:
- Assets:
php please eloquent:export-assets
- Blueprints and Fieldsets:
php please eloquent:export-blueprints
- Collections:
php please eloquent:export-collections
- Entries:
php please eloquent:export-entries
- Forms:
php please eloquent:export-forms
- Globals:
php please eloquent:export-globals
- Navs:
php please eloquent:export-navs
- Revisions:
php please eloquent:export-revisions
- Taxonomies:
php please eloquent:export-taxonomies
- Sites:
php please eloquent:export-sites
Configuration
The configuration file, found in config/statamic/eloquent-driver.php
is automatically published when you install the Eloquent Driver.
For each of the repositories, it allows you to determine if they should be driven by flat-files (file
) or Eloquent (eloquent
). Some repositories also have additional options, like the ability to override the model used.
Using dedicated columns for data
Note: This feature is currently only available for Entries.
By default, the Eloquent Driver stores all data in a single data
column. However, it is possible to store fields in their own columns.
-
First, you'll need to enable the
map_data_to_columns
option in theentries
section of the configuration file:// config/statamic/eloquent-driver.php 'entries' => [ 'driver' => 'file', 'model' => \Statamic\Eloquent\Entries\EntryModel::class, 'entry' => \Statamic\Eloquent\Entries\Entry::class, 'map_data_to_columns' => false, ],
-
Create a new migration to add the columns to the
entries
table:php artisan make:migration add_columns_to_entries_table
public function up() { Schema::create('entries', function (Blueprint $table) { $table->string('description')->nullable(); $table->json('featured_images')->nullable(); }); }
You should ensure that the column names match the field handles in your blueprints. You should also ensure the column type matches that of the fieldtype. As a general rule of thumb, here are some common mappings:
- Text fields should be stored as
string
columns. - Relationship fields should be stored as
json
columns. (Unlessmax_items
is set to1
, in which case it should be stored as astring
column.) - Number fields should be stored as
integer
ordecimal
columns.
- Text fields should be stored as
-
Run the migration:
php artisan migrate
-
If you're adding
json
orinteger
columns, you will need to provide your ownEntry
model in order to set the appropriate casts. You can do this by creating a new model which extends the defaultEntry
model:<?php namespace App\Models; class Entry extends \Statamic\Eloquent\Entries\EntryModel { protected $casts = [ // The casts from Statamic's base model... 'date' => 'datetime', 'data' => 'json', 'published' => 'boolean', // Your custom casts... 'featured_images' => 'json', ]; }
If you're using UUIDs as your entry IDs (which is the default if you imported existing entries into the database), you should extend the
Statamic\Eloquent\Entries\UuidEntryModel
class instead:class Entry extends \Statamic\Eloquent\Entries\UuidEntryModel
-
If you have existing entries, you will need to re-save them to populate the new columns. You can do this by pasting the following snippet into
php artisan tinker
:\Statamic\Facades\Entry::all()->each->save();
-
And that's it! Statamic will now read and write data to the new columns in the
entries
table, rather than thedata
column.
Upgrading
After updating to a new version of the Eloquent Driver, please ensure you run php artisan migrate
to update your database to the latest schema.
Questions
Can I store users in the database too?
By default, Statamic users live in the users
directory of your project. If you wish to move them to the database, please follow this guide.
Can I store some collections in the database, while keeping others in flat-files?
This driver does not make it possible to have some collections flat-file driven and others Eloquent driven. If you're looking for that, you may want to checkout the Runway addon, which is part of The Rad Pack.