main

filamentphp/demo

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

ListOrders.php

TLDR

This file is a part of the Demo Projects project and is located at app/Filament/Resources/Shop/OrderResource/Pages/ListOrders.php. It contains a class named ListOrders that extends the ListRecords class. The ListOrders class uses the ExposesTableToWidgets trait and has several methods for defining actions, header widgets, and tabs.

Methods

getActions

This method returns an array of actions to be displayed for the list of orders. Currently, it only returns a single CreateAction.

getHeaderWidgets

This method returns an array of header widgets to be displayed for the list of orders. It retrieves the widgets from the OrderResource class.

getTabs

This method returns an array of tabs to be displayed for filtering the list of orders. Each tab represents a different order status and uses a query condition to filter the orders accordingly.

Classes

ListOrders

This class is a subclass of the ListRecords class and represents the page for listing orders. It uses the ExposesTableToWidgets trait to expose a table to the widgets. It defines the getActions, getHeaderWidgets, and getTabs methods to customize the actions, header widgets, and tabs for the list of orders.

<?php

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

use App\Filament\Resources\Shop\OrderResource;
use Filament\Actions;
use Filament\Pages\Concerns\ExposesTableToWidgets;
use Filament\Resources\Pages\ListRecords;

class ListOrders extends ListRecords
{
    use ExposesTableToWidgets;

    protected static string $resource = OrderResource::class;

    protected function getActions(): array
    {
        return [
            Actions\CreateAction::make(),
        ];
    }

    protected function getHeaderWidgets(): array
    {
        return OrderResource::getWidgets();
    }

    public function getTabs(): array
    {
        return [
            null => ListRecords\Tab::make('All'),
            'new' => ListRecords\Tab::make()->query(fn ($query) => $query->where('status', 'new')),
            'processing' => ListRecords\Tab::make()->query(fn ($query) => $query->where('status', 'processing')),
            'shipped' => ListRecords\Tab::make()->query(fn ($query) => $query->where('status', 'shipped')),
            'delivered' => ListRecords\Tab::make()->query(fn ($query) => $query->where('status', 'delivered')),
            'cancelled' => ListRecords\Tab::make()->query(fn ($query) => $query->where('status', 'cancelled')),
        ];
    }
}