Free·

Show a Modal with Custom Content, if necessary, after Create / Edit

Sometimes, after creating or editing a record, you might want to show a modal with custom content

Filament resources make it easy to create and edit records.

The Problem

Sometimes, after creating or editing a record, you might want to show a modal with custom content, like next steps for the user, or a warning message.

The Solution

We can achieve this by creating a custom widget for the modal, which is normally hidden. We may then trigger it from the Create and Edit pages of the resource, depending on our custom logic.

app/Filament/Resources/PurchaseOrderResource/Pages/CreatePurchaseOrder.php.php
<?php

namespace App\Filament\Resources\PurchaseOrderResource\Pages;

use Illuminate\Support\Facades\Session;
use Filament\Resources\Pages\CreateRecord;
use App\Filament\Resources\PurchaseOrderResource;
use App\Filament\Resources\PurchaseOrderResource\Widgets\HighValuePurchaseOrder;

class CreatePurchaseOrder extends CreateRecord
{
    protected static string $resource = PurchaseOrderResource::class;

    protected function afterCreate(): void
    {
        if (HighValuePurchaseOrder::isNeededToShow($this->record)) {
            Session::flash(HighValuePurchaseOrder::TRIGGER);
        }
    }
}