Hello, all. Rank beginner here, but really enjoying the Cockpit experience overall so far.
I’m trying to add entries to a collection using PHP and the bootstrap.php methods, but I fear I have the syntax wrong somewhere.
Here is my attempt:
echo "<h1> TITLE: ". $posts[1][2] . "</h1>";
$return = cockpit('collections')->save('blog', [
'data' => [
'published' => true,
'slug' => $posts[1][10],
'date' => $posts[1][1],
'title' => $posts[1][2],
'image' => [
'path' => $posts[1][5]
],
'excerpt' => $posts[1][3],
'content' => $posts[1][4]
]
]);
I get an error back that the title is required (it is), but I output the title above and it looks fine. Any help is greatly appreciated!
Don’t use the named data key, just pass a single entry or an array of entries. Your example results in an entry with a single field named “data” and then the “title” is missing.
// single entry
$return = cockpit('collections')->save('blog',
[
'published' => true,
'slug' => $posts[1][10],
'date' => $posts[1][1],
'title' => $posts[1][2],
// ...
]
);
or:
// multiple entries - syntax works with single entry, too
$return = cockpit('collections')->save('blog', [
[
'published' => true,
'slug' => $posts[1][10],
'date' => $posts[1][1],
'title' => $posts[1][2],
// ...
],
[
'published' => true,
'slug' => $posts[2][10],
'date' => $posts[2][1],
'title' => $posts[2][2],
// ...
]
]);
See also:
'save' => function($collection, $data, $options = []) {
$options = array_merge(['revision' => false], $options);
$_collection = $this->collection($collection);
if (!$_collection) return false;
$name = $collection;
$collection = $_collection['_id'];
$data = isset($data[0]) ? $data : [$data];
$return = [];
$modified = time();
foreach ($data as &$entry) {
$isUpdate = isset($entry['_id']);
$entry['_modified'] = $modified;
if (isset($_collection['fields'])) {
1 Like