Runway for Statamic

website·Indexed 32 days ago

https://runway.duncanmcclean.com/

A tool to manage Eloquent models within Statamic. It provides integration for the Statamic Control Panel, front-end routing, and support for GraphQL and REST APIs. Key features include Blueprints for content modeling, Eloquent relationship management, revisions, and Antlers templating tags. Requires Statamic 5, Laravel 10+, and PHP 8.2+.

Tokens
10.2K
Snippets
79
Records
94
Agent score
62%

What's inside Runway

  1. Understand Blueprints in Runway

    Blueprints in Runway are used for content modeling. They define the fields available in the Control Panel and determine how data is stored in the database. Every resource in Runway requires its own blueprint, which can be managed directly via the Control Panel.
  2. Scope Runway model results to the currently selected Statamic site

    Runway does not provide built-in multi-site support by default to remain unopinionated about model creation. To restrict model results on a Runway listing page to the currently selected site, you must add a site handle column to your model and implement a scoped query using the Site::selected()->handle() method.
    // app/Models/Post.php
    
    use Illuminate
    Database
    Eloquent
    Attributes
    Scope;
    use Illuminate
    Database
    Eloquent
    Builder;
    
    #[Scope]
    protected function runwayListing(Builder $query): void
    {
        // Replace 'site' with the actual handle of your site column
        $query->where('site', Site::selected()->handle());
    }
  3. Upgrade Runway from v4.x to v5.0

    v4-x-to-v5-0
    To upgrade Runway from v4.x to v5.0, update the version constraint in composer.json, run the composer update command, and clear the application caches. It is strongly recommended to upgrade one major version at a time (e.g., do not skip from v3 directly to v5) and test the installation locally before deploying to production.
    "doublethreedigital/runway": "^5.0"
    composer update doublethreedigital/runway --with-dependencies
    php artisan route:clear
    php artisan view:clear
  4. Enable content revisions in Runway

    To enable the revisions feature in Runway, you must complete three steps: enable Statamic revisions in the environment, enable Runway publish states for the specific model, and activate the revisions config option for that resource. Publish states are a prerequisite and require the model to have a way to be published or unpublished (e.g., a boolean column).
    STATAMIC_REVISIONS_ENABLED=true
    // config/runway.php
    'resources' => [
        \App\Models\Order::class => [
            'name' => 'Orders',
            'published' => true, // Assumes the model has a `published` boolean column, or specify the column name
            'revisions' => true,
        ],
    ],
  5. Nest blueprint fields inside JSON columns

    To avoid creating a database migration for every new field, you can store fields within JSON columns. This requires three steps:

    1. Configure the JSON column prefix in config/runway.php under the nested_field_prefixes key for the specific model.
    2. Define a cast for the JSON column in your Eloquent model (e.g., as array, json, or AsArrayObject::class).
    3. In the blueprint, prefix the field handle with the column name (e.g., address_street_name to store street_name inside the address column).
    // 1. config/runway.php
    'resources' => [
        Order::class => [
            'nested_field_prefixes' => [
                'address',
            ],
        ],
    ],
    
    // 2. Eloquent Model
    protected function casts(): array
    {
        return [
            'address' => 'array',
        ];
    }
    // 3. Blueprint
    -
      handle: address_street_name
      field:
        type: text
        display: 'Street Name'
  6. Implement Computed Values using Eloquent Accessors

    Runway supports computed values by using public accessors on your Eloquent model. Once the accessor is defined, set the field's visibility to "Computed" in the resource's blueprint. Note that computed values are processed after the query is executed and therefore cannot be searched, sorted, or filtered.
    use Illuminate\Database\Eloquent\Casts\Attribute;
    
    public function fullName(): Attribute
    {
        return Attribute::make(
            get: function () {
                return "{$this->first_name} {$this->last_name}";
            }
        );
    }
  7. Manage 'Belongs To' relationships in Runway

    Runway provides a belongs_to fieldtype to manage Eloquent belongsTo relationships. To implement this, define the relationship in your Laravel model and add the corresponding field to your Statamic blueprint. The field handle must match the database column name.
    // Model definition
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\Relations\BelongsTo;
    
    class Post extends Model
    {
        public function author(): BelongsTo
        {
            return $this->belongsTo(Author::class);
        }
    }
    # Blueprint configuration
    - handle: author_id
      field:
        type: belongs_to
        display: Author
        resource: author
  8. Store Statamic revisions in the database

    By default, Statamic stores revisions in the storage/statamic/revisions directory. To store them in the database instead, install the official Eloquent Driver and select the 'Revisions' repository.
    php please install:eloquent-driver
  9. System requirements for Runway v9.0

    v8-to-v9
    Runway v9.0 increases the minimum required versions for PHP and Laravel. Apps using PHP < 8.2 or Laravel < 11 must be upgraded. It is highly recommended to use PHP 8.4 and Laravel 12.
    Minimum PHP: 8.2
    Minimum Laravel: 11
    Recommended PHP: 8.4
    Recommended Laravel: 12
  10. Upgrade Runway from v5.x to v6.0

    v5-x-to-v6-0

    To upgrade from Runway v5.x to v6.0, follow these steps in order. Do not skip versions (e.g., do not upgrade directly from v4 to v6). Test all changes locally before deploying to production.

    1. Re-install under the Rad Pack namespace: Uninstall the old package and install the new one.
    2. Update Model Traits: Update the namespaces for HasRunwayResource and RunwayRoutes in your Eloquent models.
    3. Migrate Blueprints: Run the migration command to move blueprints to the new directory structure so they are editable in the Control Panel.
    4. Clear Caches: Clear route and view caches.
    # 1. Re-install
    composer remove doublethreedigital/runway
    composer require statamic-rad-pack/runway:^6.0
    
    # 3. Migrate blueprints
    php please runway:migrate-blueprints
    
    # 4. Clear caches
    php artisan route:clear
    php artisan view:clear
  11. Customize resource action authorization

    To change how resource actions are authorized, extend the StatamicRadPack\Runway\Policies\ResourcePolicy class and rebind it in your AppServiceProvider.
    // app/Policies/ResourcePolicy.php
    namespace App\Policies;
    
    use StatamicRadPack\Runway\Policies\ResourcePolicy as RunwayResourcePolicy;
    use Illuminate\Auth\Access\HandlesAuthorization;
    
    class ResourcePolicy extends RunwayResourcePolicy
    {
        use HandlesAuthorization;
    
        public function edit($user, $resource)
        {
            // Custom checks here
            return parent::edit($user, $resource);
        }
    }
    
    // app/Providers/AppServiceProvider.php
    public function boot()
    {
        $this->app->bind(\StatamicRadPack\Runway\Policies\ResourcePolicy::class, \App\Policies\ResourcePolicy::class);
    }
  12. Retrieve Runway resource data using the runway Antlers tag

    Runway provides a {{ runway:resource_name }} Antlers tag (or <s:runway:resource_name> Blade component) to fetch model data for specific resources. This allows you to loop through records and display their fields in your templates.
    {{ runway:post }}
    	<h2>{{ title }}</h2>
    	<p>{{ intro_text }}</p>
    {{ /runway:post }}
    <s:runway:post>
    	<h2>{{ $title }}</h2>
    	<p>{{ $intro_text }}</p>
    </s:runway:post>