You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+77-3Lines changed: 77 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -58,9 +58,83 @@ The configuration file, found in `config/statamic/eloquent-driver.php` is automa
58
58
59
59
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.
60
60
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:
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.
0 commit comments