Save collection entry with bootstrap / PHP

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:

1 Like