main

filamentphp/demo

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

LatestOrders.php

TLDR

This file contains the code for the LatestOrders widget, which is a table widget that displays the latest orders in a shop. The widget is a subclass of the TableWidget class and defines a table method for customizing the behavior and appearance of the table.

Classes

LatestOrders

The LatestOrders class is a subclass of the TableWidget class and represents a widget that displays a table of the latest orders in a shop. It has the following properties and methods:

  • Properties
    • $columnSpan (int | string | array): Specifies the column span of the widget. Default value is 'full'.
    • static $sort (?int): Specifies the sort order of the widget. Default value is 2.
  • Methods
    • table(Table $table): Table: Configures the table associated with the widget. It sets up the query, pagination, default sort order, columns, and actions of the table. Returns the modified table object.
<?php

namespace App\Filament\Widgets;

use App\Filament\Resources\Shop\OrderResource;
use App\Models\Shop\Order;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;
use Squire\Models\Currency;

class LatestOrders extends BaseWidget
{
    protected int | string | array $columnSpan = 'full';

    protected static ?int $sort = 2;

    public function table(Table $table): Table
    {
        return $table
            ->query(OrderResource::getEloquentQuery())
            ->defaultPaginationPageOption(5)
            ->defaultSort('created_at', 'desc')
            ->columns([
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Order Date')
                    ->date()
                    ->sortable(),
                Tables\Columns\TextColumn::make('number')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('customer.name')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('status')
                    ->badge(),
                Tables\Columns\TextColumn::make('currency')
                    ->getStateUsing(fn ($record): ?string => Currency::find($record->currency)?->name ?? null)
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('total_price')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('shipping_price')
                    ->label('Shipping cost')
                    ->searchable()
                    ->sortable(),
            ])
            ->actions([
                Tables\Actions\Action::make('open')
                    ->url(fn (Order $record): string => OrderResource::getUrl('edit', ['record' => $record])),
            ]);
    }
}