Data Factories

When you need to create a new data object, you shouldn't be "new-ing" it up like: new Page. Data Factories exist to make your life easier.


If you haven’t already, please consider reading an overview of managing data.

Statamic has some data factories that provide you with a nice fluent interface for creating them, as well as handling some additional behind-the-scenes logic automatically for you.

Each data type will have its own factory, but they all follow the same basic workflow. First, use their respective API class to start up the factory. Then, you can go ahead and chain methods onto it. Once you’re happy, you can get the object out of the factory.

Here’s what that might look like:

use Statamic\API\Entry;

Entry::create('my-post')
    ->collection('blog')
    ->with(['title' => 'My First Post'])
    ->published(false)
    ->date('2016-05-10')
    ->get();

// Returns a Statamic\Data\Entries\Entry

Of course, you don’t have to chain everything in one go:

$factory = Entry::create('my-post')
                ->collection('blog')
                ->with(['title' => 'My First Post'])
                ->date('2016-05-10');
if ($draft) {
    $factory->published(false);
}

$entry = $factory->get();

Once you have your object (an Entry, in this case) - remember that it doesn’t exist as a file until you save it. That’s simple though:

$entry->save();

Each factory starts off a little different. Entries use Entry::create($slug), Pages use Page::create($uri). Check out the docs for the factory you’re using.

Last modified on November 17, 2016