Picture of the author

Notion Databases

Create Database

Basic Creation

Without much hassle you can create a new database with basic configuration. In case no properties are given, a basic title property - which is required - will be automatically added.

    $databaseEntity = Notion::databases()
        ->build()
        ->createInPage('0adbc2eb47e84569a700a60d537615be');

The configuration of inline, title, cover, icon and description can be set as well.

    $databaseEntity = Notion::databases()
        ->build()
        ->inline() // Currently not supported due to Notion API versioning
        ->title('Created By Laravel')
        ->coverExternal('https://example.com/cover.jpg')
        ->iconExternal('https://example.com/cover.jpg')
        ->description('This Database has been created by Laravel')
        ->createInPage('0adbc2eb47e84569a700a60d537615be');

Not (yet) supported configuration

Setting a created Database as inline is currently not supported by us. There is a breaking change regarding the Notion API, which we have to resolve first. If you try to set a created database as inline it will be created without an error, however it will not be inline.

Adding Properties

There are multiple ways to add properties to your database creation. All of them encapsulate the same behaviour, so feel free to choose your personal style.

All available properties will be listed in each example.

The Property Status is currently not supported. Please refer down below.

The Bulk Way

Use the method PropertyBuilder::bulk to receive an instance of DatabaseSchemeBuilder. With this instance you can define properties similar to Laravel migrations.

use FiveamCode\LaravelNotionApi\Builder\PropertyBuilder;

$selectOptions = [
    [
        'name' => 'testing',
        'color' => 'blue',
    ],
];

$multiSelectOptions = [
    [
        'name' => 'testing2',
        'color' => 'yellow',
    ],
];

$scheme = PropertyBuilder::bulk()
        ->title('MyTitle') // Title is required
        ->richText('MyText')
        ->checkbox('...') // Name of property is (mostly) optional
        // ->status() // WARNING: currently not supported
        ->number()
        ->date()
        ->url()
        ->email()
        ->phoneNumber()
        ->people()
        ->files()
        ->createdBy()
        ->createdTime()
        ->lastEditedBy()
        ->lastEditedTime()
        ->formula('Testing', 'prop("MultiSelect")')
        ->select('Select', $selectOptions)
        ->multiSelect('MultiSelect', $multiSelectOptions);

 $databaseEntity = Notion::databases()
        ->build()
        ->inline()
        ->title('Test Database')
        ->add($scheme)
        ->createInPage('1c682e7371ec4399be1cc015686c67c6');

The Eloquent Way

To keep it as close as possible to the eloquent ways of Laravel, you can create databases similar to migrations.

$databaseEntity = Notion::databases()
    ->build()
    ->title('Test Database')
    ->scheme(function ($table) {
        $table->title('MyTitle') // Title is required
        $table->richText('MyText')
        $table->checkbox('...') // Name of property is (mostly) optional
        // $table->status() // WARNING: Currently not supported
        $table->number()
        $table->date()
        $table->url()
        $table->email()
        $table->phoneNumber()
        $table->people()
        $table->files()
        $table->createdBy()
        $table->createdTime()
        $table->lastEditedBy()
        $table->lastEditedTime()
        $table->formula('Testing', 'prop("MultiSelect")')
        $table->select('Select', $selectOptions) // please refer 'Bulk Generation'
        $table->multiSelect('MultiSelect', $multiSelectOptions); // please refer 'Bulk Generation'
    })
    ->createInPage('1c682e7371ec4399be1cc015686c67c6');

The Basic Way

Add a Collection or a PropertyBuilder instance, in order to set initial properties for your database.

use FiveamCode\LaravelNotionApi\Builder\PropertyBuilder;

$props = collect([
     PropertyBuilder::title('My Title'), // Title is required
     PropertyBuilder::richText('MyText'),  // Name of property is (mostly) optional
     PropertyBuilder::checkbox('...'),
    //PropertyBuilder::status(), // WARNING: Currently not supported
     PropertyBuilder::number(),
     PropertyBuilder::url(),
     PropertyBuilder::email(),
     PropertyBuilder::phoneNumber(),
     PropertyBuilder::people(),
     PropertyBuilder::files(),
     PropertyBuilder::createdBy(),
     PropertyBuilder::createdTime(),
     PropertyBuilder::lastEditedBy(),
     PropertyBuilder::lastEditedTime(),
     PropertyBuilder::formula('Testing', 'prop("MultiSelect")'),
     PropertyBuilder::select('Select', $selectOptions), // please refer 'Bulk Generation' 
     PropertyBuilder::multiSelect('MultiSelect', $multiSelectOptions), // please refer 'Bulk Generation'
]);

$databaseEntity = Notion::databases()
    ->build()
    ->title('Test DB 3')
    ->add($props) // add multiple properties (Collection)
    ->add(PropertyBuilder::date('My Date')) // add single property
    ->createInPage('1c682e7371ec4399be1cc015686c67c6');

Status Property (not supported)

Not (yet) supported property

The property Status is currently not supported by us. There is a breaking change regarding the Notion API, which we have to resolve first. If you try to create a Status property, you will receive a NotionException.

Previous
Fetch Database Structure