承接 a909m/filament-statefusion 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

a909m/filament-statefusion

Composer 安装命令:

composer require a909m/filament-statefusion

包简介

Filament StateFusion is a powerful FilamentPHP plugin that seamlessly integrates Spatie Laravel Model States into the Filament admin panel. This package provides an intuitive way to manage model states, transitions, and filtering within Filament, enhancing the user experience and developer productiv

README 文档

README

Description

Filament-StateFusion

Brings the full power of Spatie Laravel Model States to Filament with zero complexity and beautiful visual workflows.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Filament StateFusion is a powerful FilamentPHP plugin that seamlessly integrates Spatie Laravel Model States into the Filament admin panel. Effortlessly manage model states, transitions, and filtering within Filament, enhancing both user experience and developer productivity.

Perfect for order processing, publishing workflows, approval systems, and any application requiring well-defined state management.

Features

Rich State Management

  • Display model states in tables with colors, icons, and descriptions
  • Filter and group records by state
  • Transition between states using intuitive UI components
  • Bulk state transitions with validation

🛠 Developer Experience

  • Out-of-the-box support for Spatie Laravel Model States
  • Custom transition forms for collecting additional data
  • Automatic state validation and transition rules
  • Compatible with Filament v4 dark mode

🎨 Customizable Interface

  • Custom labels, colors, icons, and descriptions for states
  • Custom transition forms and validation
  • Flexible attribute mapping for complex models

🎬 Preview

📸 Screenshots and demo GIFs will be added soon

Requirements

This plugin is designed to work with the following dependencies:

  • PHP: ^8.1
  • Laravel: ^10.0|^11.0|^12.0
  • Filament: ^3.0|^4.0|^5.0
  • Spatie Laravel Model States: ^2.0

Installation

Install the package via Composer:

Plugin Version Filament Version Readme
1.x 3.x Link
2.x 4.x 5.x
composer require a909m/filament-statefusion

Upgrading to Filament v4

  • composer require a909m/filament-statefusion:2.0
  • The Action classes were simplified into a single StateFusionBulkAction and StateFusionAction for pages and tables

Then, implement the HasFilamentStateFusion interface and use the StateFusionInfo trait on your abstract state class.

Finally, you can start using the components and actions provided by this plugin in your Filament resources.

Understanding State Management

This plugin builds on Spatie Laravel Model States, which provides:

  • State Classes: Each state is a separate class with its own behavior and logic
  • State Transitions: Define which state changes are allowed (e.g., PendingProcessing)
  • Transition Classes: Optional classes for complex transitions that need additional data or logic
  • Type Safety: Your IDE knows which states and methods are available

Example: An order can be Pending, Processing, Shipped, or Cancelled. You define:

  • Which transitions are valid (Pending can become Processing, but Shipped cannot become Pending)
  • What happens during each transition (send emails, update timestamps, etc.)

StateFusion makes these states visual and interactive in Filament with dropdowns, badges, filters, and action buttons.

Getting Started

1. Prepare Your State Classes

First, ensure you have Spatie Laravel Model States configured. Then implement the HasFilamentStateFusion interface and use the StateFusionInfo trait on your abstract state class:

<?php
// app/States/OrderState.php
use A909M\FilamentStateFusion\Concerns\StateFusionInfo;
use A909M\FilamentStateFusion\Contracts\HasFilamentStateFusion;
use Spatie\ModelStates\State;
use Spatie\ModelStates\StateConfig;

abstract class OrderState extends State implements HasFilamentStateFusion
{
    use StateFusionInfo;

    public static function config(): StateConfig
    {
        return parent::config()
            ->default(PendingState::class)
            ->allowTransition(PendingState::class, ProcessingState::class)
            ->allowTransition(ProcessingState::class, ShippedState::class)
            ->allowTransition(ShippedState::class, DeliveredState::class)
            ->allowTransition([PendingState::class, ProcessingState::class], CancelledState::class);
    }
}

New to Spatie Laravel Model States? Read their introduction first to understand states, transitions, and the state pattern.

2. Configure Your Model

Add the state to your Eloquent model:

<?php
// app/Models/Order.php

use App\Models\States\OrderState;
use Illuminate\Database\Eloquent\Model;
use App\States\OrderState;

class Order extends Model
{
    use HasStates;

    protected $casts = [
        'status' => OrderState::class,
    ];
    
}

3. Use in Filament Resources

Now you can use StateFusion components in your Filament resources:

// app/Filament/Resources/OrderResource.php
use A909M\FilamentStateFusion\Tables\Columns\StateFusionSelectColumn;
use A909M\FilamentStateFusion\Tables\Filters\StateFusionSelectFilter;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables\Table;

class OrderResource extends Resource
{
    // ...

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                StateFusionSelectColumn::make('status'),
            ])
            ->filters([
                StateFusionSelectFilter::make('status'),
            ]);
    }
}

Usage

Form Components

You can use the following state-aware components in your forms:

  • StateFusionSelect
  • StateFusionRadio
  • StateFusionToggleButtons
use A909M\FilamentStateFusion\Forms\Components\StateFusionSelect;
use A909M\FilamentStateFusion\Forms\Components\StateFusionRadio;
use A909M\FilamentStateFusion\Forms\Components\StateFusionToggleButtons;

// Dropdown select
StateFusionSelect::make('status'),

// Radio buttons with descriptions
StateFusionRadio::make('status'),

// Toggle buttons with colors and icons
StateFusionToggleButtons::make('status'),

Table Columns

StateFusionSelectColumn

Display state information in your Filament tables and allow for quick state transitions.

use A909M\FilamentStateFusion\Tables\Columns\StateFusionSelectColumn;

StateFusionSelectColumn::make('status'),

TextColumn

You can also use the standard TextColumn to display the state as a badge. When your state classes implement HasLabel, HasColor, and HasIcon interfaces, the badge will automatically display the appropriate label, color, and icon without any additional configuration.

use Filament\Tables\Columns\TextColumn;

TextColumn::make('status')
    ->badge(),

Table Filter

Filter records by state:

use A909M\FilamentStateFusion\Tables\Filters\StateFusionSelectFilter;

StateFusionSelectFilter::make('status'),

Table Actions

StateFusionTableAction

Add state transition actions to your table rows.

use A909M\FilamentStateFusion\Tables\Actions\StateFusionTableAction;

StateFusionTableAction::make('approve')
    ->transitionTo(ApprovedState::class),

StateFusionBulkAction

Transition multiple records at once.

use A909M\FilamentStateFusion\Tables\Actions\StateFusionBulkAction;

StateFusionBulkAction::make('approve')
    ->transition(PendingState::class,ApprovedState::class),

Infolist Entries

TextEntry

Similar to the table column, you can use the standard TextEntry in your infolists to display the model state as a badge. When your state classes implement HasLabel, HasColor, and HasIcon interfaces, the badge will automatically display the appropriate label, color, and icon without any additional configuration.

use Filament\Infolists\Components\TextEntry;

TextEntry::make('status')
    ->badge(),

Page Actions

StateFusionAction

Create actions to transition between states from a page.

use A909M\FilamentStateFusion\Actions\StateFusionAction;

StateFusionAction::make('approve')
    ->transitionTo(ApprovedState::class),

Customization

Custom Attributes

By default, the state components use the first attribute defined in your model's getDefaultStates() as the state attribute. If your model uses a different attribute name or you want to specify which attribute to use, you can set it using the attribute() method.

// Using a different attribute name
StateFusionSelect::make('approval_status')
    ->attribute('approval_status'),

// Dynamic attribute selection
StateFusionAction::make('transition')
    ->attribute('state')
    ->transitionTo(ApprovedState::class),

This is useful when your model has multiple state attributes or you want to reuse the component for different attributes.

Customizing States

To customize how states appear in the UI, implement the HasLabel, HasDescription, HasColor, or HasIcon interfaces on your concrete state classes:

use Filament\Support\Colors\Color;
use Filament\Support\Contracts\HasColor;
use Filament\Support\Contracts\HasDescription;
use Filament\Support\Contracts\HasIcon;
use Filament\Support\Contracts\HasLabel;

final class CancelledState extends OrderState implements HasDescription, HasColor, HasIcon, HasLabel
{
    public function getLabel(): string
    {
        return __('Order Cancelled');
    }

    public function getColor(): array
    {
        return Color::Red;
    }

    public function getIcon(): string
    {
        return 'heroicon-o-x-circle';
    }

    public function getDescription(): ?string
    {
        return 'Order cancelled, transaction reversed.';
    }
}

Customizing Transitions

Similarly, transitions can be customized by implementing the same interfaces:

use Filament\Support\Colors\Color;
use Filament\Support\Contracts\HasColor;
use Filament\Support\Contracts\HasIcon;
use Filament\Support\Contracts\HasLabel;
use Spatie\ModelStates\Transition;

final class ToCancelled extends Transition implements HasLabel, HasColor, HasIcon
{
    public function getLabel(): string
    {
        return __('Mark as Cancelled');
    }

    public function getColor(): array
    {
        return Color::Red;
    }

    public function getIcon(): string
    {
        return 'heroicon-o-x-circle';
    }
}

Custom Transitions with Forms

Sometimes you need to collect additional information when transitioning between states. StateFusion makes this easy by letting you add a form() method to your custom transition classes.

Example: Cancelling an order with a reason

<?php

use Filament\Forms\Components\Textarea;
use Filament\Support\Colors\Color;
use Filament\Support\Contracts\{HasLabel, HasColor, HasIcon};
use Spatie\ModelStates\Transition;

final class CancelOrder extends Transition implements HasLabel, HasColor, HasIcon
{
    public function __construct(
        private Order $order,
        private ?array $data = null
    ) {}

    public function handle(): Order
    {
        $this->order->state = new CancelledState($this->order);
        $this->order->cancellation_reason = $this->data['reason'];
        $this->order->cancelled_at = now();
        $this->order->save();

        return $this->order;
    }

    public function form(): array
    {
        return [
            Textarea::make('reason')
                ->label('Cancellation Reason')
                ->placeholder('Why are you cancelling this order?')
                ->required()
                ->maxLength(500),
        ];
    }

    public function getLabel(): string
    {
        return 'Cancel Order';
    }

    public function getColor(): string | array
    {
        return Color::Red;
    }

    public function getIcon(): string
    {
        return 'heroicon-o-x-circle';
    }
}

The form data is automatically passed to your transition's $data parameter, which you can then use in the handle() method to update your model.

Learn more: Custom Transition Classes in the Spatie documentation.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details on how to contribute to this project.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

a909m/filament-statefusion 适用场景与选型建议

a909m/filament-statefusion 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.06k 次下载、GitHub Stars 达 30, 最近一次更新时间为 2025 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「admin-panel」 「state-machine」 「spatie」 「filament」 「state-management」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 a909m/filament-statefusion 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 a909m/filament-statefusion 我们能提供哪些服务?
定制开发 / 二次开发

基于 a909m/filament-statefusion 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 18.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 31
  • 点击次数: 44
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 30
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-10