Notion for Laravel Documentation

website·Indexed 32 days ago

https://notionforlaravel.com/

Documentation for the Laravel Notion API package (v1.1.0), providing tools to interact with Notion databases, pages, blocks, and comments. Includes guides on filtering, sorting, pagination, error handling with NotionException, and managing Notion users and entities.

Tokens
10K
Snippets
72
Records
84
Agent score
63%

What's inside Notion for Laravel

  1. Retrieve parent information for Notion entities

    v1.1.0
    The ParentId and ParentType properties provide information about the parent page or block for a specific item. This metadata is available for Database, Page, Block, and Comment entities.
  2. Create and set Notion page properties

    v1.1.0
    To set a property on a Notion page, use the dedicated property classes located in the FiveamCode\LaravelNotionApi\Entities\Properties namespace. Each class provides a value() method to instantiate the property with the correct structure before passing it to the page's setProperty() method.
    use FiveamCode\LaravelNotionApi\Entities\Properties\Text;
    
    // 1. Create the property object
    $textProperty = Text::value('This is a Text Property');
    
    // 2. Assign it to the Notion page
    $notionPage->setProperty('Text Property', $textProperty);
  3. Retrieve all Notion users in a workspace

    v1.1.0
    Use the all() function on the Notion::users() instance to retrieve all users in the connected workspace. Note that a maximum of 100 users can be fetched using this method. Results can be converted to a collection or JSON.
    Notion::users()
            ->all()
            ->asCollection(); // or `asJson()`
  4. Retrieve and read Notion page properties

    v1.1.0

    You can retrieve a specific property by its title or get all properties as a collection from a Notion page. If a property type is supported by the package, it returns a specific property class (e.g., \Entities\Properties\Text); otherwise, it returns a generic \Entities\Properties\Property object containing the raw Notion API structure.

    All property objects provide these general methods:

    • getTitle(): Returns the property title.
    • getType(): Returns the property type.
    • getContent(): Returns the base content (raw content if unsupported).
    • getRawContent(): Returns the raw Notion API content.
    • asText(): Returns the content as a string (JSON string if unsupported).
    $propertyName = 'Your Property Title';
    
    // Get a specific property
    $property = $page->getProperty($propertyName);
    
    // Retrieve all properties as an Illuminate\Support\Collection
    $collectionOfProperties = $page->getProperties();
  5. Install laravel-notion-api via Composer

    Install the Notion API package for Laravel using Composer. After installation, you must obtain a Notion API access token from the Notion developer documentation and grant the integration access to the specific Notion pages you intend to manage.
    composer require fiveam-code/laravel-notion-api
  6. Append a block to a Notion page

    v1.1.0
    To add a new block to the end of a Notion page, use the append method on a block instance. The parent block ID required for this operation is the UUID of the target Notion Page.
    use FiveamCode\LaravelNotionApi\Entities\Blocks\Paragraph;
    
    // The parent block ID is the UUID of the Notion Page
    $parentBlockId = 'f7a7c7f7-7f7f-7f7f-7f7f-7f7f7f7f7f7f';
    
    // Create the block content
    $newBlock = Paragraph::create("New TextBlock");
    
    // Append the block to the page
    Notion::block($parentBlockId)->append($newBlock);
  7. Create a basic Notion database in a page

    v1.1.0
    Create a new Notion database within a specific page using the Notion::databases() builder. If no properties are specified, a required title property is automatically added. You can optionally configure the database title, cover image, icon, and description.
    // Basic creation
    $databaseEntity = Notion::databases()
        ->build()
        ->createInPage('0adbc2eb47e84569a700a60d537615be');
    
    // Creation with configuration
    $databaseEntity = Notion::databases()
        ->build()
        ->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');
  8. Create a Notion page within a database

    v1.1.0
    Create a new Notion page within a database using the createInDatabase method. Pages created in databases can have custom properties, provided they match the database structure. Properties not explicitly set will default to the database's default value (usually empty).
    use FiveamCode\LaravelNotionApi\Entities\Properties\Title;
    use FiveamCode\LaravelNotionApi\Entities\Page;
    use FiveamCode\LaravelNotionApi\Facades\Notion;
    
    // The UUID of the parent Notion Database
    $parentDatabaseId = 'f7a7c7f7-7f7f-7f7f-7f7f-7f7f7f7f7f7f';
    
    $page = new Page();
    $page->set('title', Title::value('I was created from Laravel'));
    
    Notion::pages()->createInDatabase($parentDatabaseId, $page);
  9. Build complex queries with FilterBag

    v1.1.0

    A FilterBag allows you to combine multiple filters using AND or OR logic.

    • Creation: Use FilterBag::and() or FilterBag::or() for quick instantiation.
    • Adding Content: Use addFilter() for individual filters, addFilterBag() for nested bags, or addFilters() for a collection of filters.
    • Nesting Constraint: The maximum nesting level is 2. A FilterBag can contain another FilterBag, but that child bag cannot contain further bags. Exceeding this limit throws a HandlingException.
    use FiveamCode\LaravelNotionApi\Query\Filters\Filter;
    use FiveamCode\LaravelNotionApi\Query\Filters\FilterBag;
    use FiveamCode\LaravelNotionApi\Query\Filters\Operators;
    
    // Condition: ('Known for' CONTAINS 'Univac' AND ('Name' EQUALS 'Grace' OR 'Name' EQUALS 'Jean'))
    $filterBag = new FilterBag(Operators::AND);
    
    $filterBag->addFilter(
        Filter::rawFilter("Known for", ["multi_select" => [Operators::CONTAINS => "UNIVAC']])
    );
    
    $nameFilterBag = new FilterBag(Operators::OR);
    $nameFilterBag
        ->addFilter(Filter::textFilter("Name", Operators::EQUALS, "Grace Hopper"))
        ->addFilter(Filter::textFilter("Name", Operators::EQUALS, "Jean Bartik"));
    
    $filterBag->addFilterBag($nameFilterBag);
    
    Notion::database("DATABASE_ID")
        ->filterBy($filterBag)
        ->query()
        ->asCollection();
  10. Update an existing Notion page

    v1.1.0
    Update an existing Notion page by providing the page UUID. If you have a retrieved Page object, you can use it directly; otherwise, instantiate a new Page object and set the ID manually before calling the update method.
    use FiveamCode\LaravelNotionApi\Entities\Properties\Title;
    use FiveamCode\LaravelNotionApi\Entities\Page;
    use FiveamCode\LaravelNotionApi\Facades\Notion;
    
    $page = new Page();
    $page->setId($pageId); // Set the UUID of the page to update
    
    // Set the properties to be updated
    $page->set('title', Title::value('Updated Title'));
    
    Notion::pages()->update($page);