PHPackages                             statamic/eloquent-driver - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. statamic/eloquent-driver

ActiveLibrary[Database &amp; ORM](/categories/database)

statamic/eloquent-driver
========================

Allows you to store Statamic data in a database.

v5.5.0(1mo ago)125598.8k—3.4%97[12 issues](https://github.com/statamic/eloquent-driver/issues)[4 PRs](https://github.com/statamic/eloquent-driver/pulls)7MITPHPPHP ^8.3CI passing

Since Nov 19Pushed 1mo ago8 watchersCompare

[ Source](https://github.com/statamic/eloquent-driver)[ Packagist](https://packagist.org/packages/statamic/eloquent-driver)[ RSS](/packages/statamic-eloquent-driver/feed)WikiDiscussions 5.x Synced 1mo ago

READMEChangelog (10)Dependencies (11)Versions (135)Used By (7)

Eloquent Driver
===============

[](#eloquent-driver)

> Provides support for storing your Statamic data in a database, rather than flat files.

Installation &amp; Usage
------------------------

[](#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

[](#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:

- Addon Settings: `php please eloquent:import-addon-settings`
- 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

[](#assets)

#### Syncing

[](#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

[](#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:

- Addon Settings: `php please eloquent:export-addon-settings`
- 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
-------------

[](#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

[](#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.

1. First, you'll need to enable the `map_data_to_columns` option in the `entries` 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,
    ],
    ```
2. 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. (Unless `max_items` is set to `1`, in which case it should be stored as a `string` column.)
    - Number fields should be stored as `integer` or `decimal` columns.
3. Run the migration:

    ```
    php artisan migrate
    ```
4. If you're adding a column that [requires an Eloquent cast](https://laravel.com/docs/master/eloquent-mutators#attribute-casting) (eg. a `json` or `integer` column), you will need to provide your own `Entry` model in order to set the appropriate casts. You can do this by creating a new model which extends the default `Entry` model:

    ```
