main

filamentphp/demo

Last updated at: 29/12/2023 09:53

CreateOrder.php

TLDR

This file is a part of the Demo Projects project and is located at app/Filament/Resources/Shop/OrderResource/Pages/CreateOrder.php. It contains a class called CreateOrder which extends the CreateRecord class. This class implements a form for creating an order and defines the steps and schema for the form.

Methods

form

This method is used to define the form for creating an order. It returns the parent form method from the CreateRecord class and adds a schema for a wizard component to the form.

afterCreate

This method is called after a new order is created. It sends a notification to the database with details about the new order.

getSteps

This method returns an array of steps for the wizard component in the form. Each step has a name and a schema defining the fields and sections for that step.

Class

CreateOrder

This class extends the CreateRecord class and implements a form for creating an order. It also defines the steps and schema for the form.

<?php

namespace App\Filament\Resources\Shop\OrderResource\Pages;

use App\Filament\Resources\Shop\OrderResource;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Wizard;
use Filament\Forms\Components\Wizard\Step;
use Filament\Forms\Form;
use Filament\Notifications\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord;
use Filament\Resources\Pages\CreateRecord\Concerns\HasWizard;

class CreateOrder extends CreateRecord
{
    use HasWizard;

    protected static string $resource = OrderResource::class;

    public function form(Form $form): Form
    {
        return parent::form($form)
            ->schema([
                Wizard::make($this->getSteps())
                    ->startOnStep($this->getStartStep())
                    ->cancelAction($this->getCancelFormAction())
                    ->submitAction($this->getSubmitFormAction())
                    ->skippable($this->hasSkippableSteps())
                    ->contained(false),
            ])
            ->columns(null);
    }

    protected function afterCreate(): void
    {
        $order = $this->record;

        Notification::make()
            ->title('New order')
            ->icon('heroicon-o-shopping-bag')
            ->body("**{$order->customer->name} ordered {$order->items->count()} products.**")
            ->actions([
                Action::make('View')
                    ->url(OrderResource::getUrl('edit', ['record' => $order])),
            ])
            ->sendToDatabase(auth()->user());
    }

    protected function getSteps(): array
    {
        return [
            Step::make('Order Details')
                ->schema([
                    Section::make()->schema(OrderResource::getDetailsFormSchema())->columns(),
                ]),

            Step::make('Order Items')
                ->schema([
                    Section::make()->schema([
                        OrderResource::getItemsRepeater(),
                    ]),
                ]),
        ];
    }
}