Sample Data
Build messages with sample data and add named variants.
Messages without constructor arguments render as they are. For other messages, add a public static previewConfig() method that returns a PreviewConfig. The method needs no interface, so the class still loads in production, where the package is not installed.
Build the message
Pass a closure that returns the message to withMessage():
use App\Models\Order;
use HosmelQ\MessagePreview\Support\PreviewConfig;
use Illuminate\Mail\Mailable;
final class OrderShipped extends Mailable
{
public function __construct(public Order $order) {}
public static function previewConfig(): PreviewConfig
{
return PreviewConfig::configure()
->withMessage(fn (): self => new self(Order::factory()->make()));
}
}The closure must return an instance of the class. Use factories with make(), since create() writes to the database.
Add variants
Pass a name and a closure to withVariant():
return PreviewConfig::configure()
->withMessage(fn (): self => new self(Order::factory()->make()))
->withVariant('multiple-packages', fn (): self => new self(
Order::factory()->hasPackages(3)->make(),
));Variant names start with a lowercase letter and contain only lowercase letters, numbers, dashes, and underscores. The preview labels multiple-packages as Multiple packages.
The closures run only when their variant is selected.
Factories return the same data on every render of a message and variant, so reloading the preview shows only what changed in the template. Values from Str::random(), UUIDs, and the current time still change.