Skip to content

Commit 5fa84f7

Browse files
Improve data column mapping docs (#340)
* Improve data column mapping docs * indent * use correct config handle --------- Co-authored-by: Ryan Mitchell <[email protected]>
1 parent 3abadd5 commit 5fa84f7

File tree

1 file changed

+77
-3
lines changed

1 file changed

+77
-3
lines changed

README.md

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,83 @@ The configuration file, found in `config/statamic/eloquent-driver.php` is automa
5858

5959
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.
6060

61-
### Mapping Entry data
62-
63-
If you want to map fields from your blueprints to columns with the same handle in your blueprint, set `entries.map_data_to_columns` to true. When adding new columns in a migration we recommend resaving all Entries so that column data is filled: `Entry::all()->each->save()`.
61+
### Using dedicated columns for data
62+
63+
> Note: This feature is currently only available for Entries.
64+
65+
By default, the Eloquent Driver stores all data in a single `data` column. However, it is possible to store fields in their own columns.
66+
67+
1. First, you'll need to enable the `map_data_to_columns` option in the `entries` section of the configuration file:
68+
69+
```php
70+
// config/statamic/eloquent-driver.php
71+
72+
'entries' => [
73+
'driver' => 'file',
74+
'model' => \Statamic\Eloquent\Entries\EntryModel::class,
75+
'entry' => \Statamic\Eloquent\Entries\Entry::class,
76+
'map_data_to_columns' => false,
77+
],
78+
```
79+
80+
2. Create a new migration to add the columns to the `entries` table:
81+
```bash
82+
php artisan make:migration add_columns_to_entries_table
83+
```
84+
85+
```php
86+
public function up()
87+
{
88+
Schema::create('entries', function (Blueprint $table) {
89+
$table->string('description')->nullable();
90+
$table->json('featured_images')->nullable();
91+
});
92+
}
93+
```
94+
95+
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:
96+
97+
* Text fields should be stored as `string` columns.
98+
* 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.)
99+
* Number fields should be stored as `integer` or `decimal` columns.
100+
101+
3. Run the migration:
102+
```bash
103+
php artisan migrate
104+
```
105+
106+
4. If you're adding `json` or `integer` columns, 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:
107+
```php
108+
<?php
109+
110+
namespace App\Models;
111+
112+
class Entry extends \Statamic\Eloquent\Entries\EntryModel
113+
{
114+
protected $casts = [
115+
// The casts from Statamic's base model...
116+
'date' => 'datetime',
117+
'data' => 'json',
118+
'published' => 'boolean',
119+
120+
// Your custom casts...
121+
'featured_images' => 'json',
122+
];
123+
}
124+
```
125+
126+
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:
127+
128+
```php
129+
class Entry extends \Statamic\Eloquent\Entries\UuidEntryModel
130+
```
131+
132+
5. 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`:
133+
```php
134+
\Statamic\Facades\Entry::all()->each->save();
135+
```
136+
137+
6. And that's it! Statamic will now read and write data to the new columns in the `entries` table, rather than the `data` column.
64138

65139
## Upgrading
66140

0 commit comments

Comments
 (0)