abather/spatie-laravel-model-states-actions 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

abather/spatie-laravel-model-states-actions

Composer 安装命令:

composer require abather/spatie-laravel-model-states-actions

包简介

This is my package spatie-laravel-model-states-actions

README 文档

README

Latest Version on Packagist Total Downloads

with this package you can display or add actions to your views using state, this package depends on spatie/laravel-model-states package.

Installation

You can install the package via composer:

composer require abather/spatie-laravel-model-states-actions

Usage

Follow the Doc in Laravel-model-states , but for each State you have to extends Abather\SpatieLaravelModelStatesActions\State :

use Abather\SpatieLaravelModelStatesActions\State;
use Spatie\ModelStates\StateConfig;

abstract class PaymentState extends State
{   
    public static function config(): StateConfig
    {
        return parent::config()
            ->default(Pending::class)
            ->allowTransition(Pending::class, Paid::class)
            ->allowTransition(Pending::class, Failed::class)
        ;
    }
}

configure authorization

you can define ability name for each state that well be used to determine if the user can change the state or not as:

<?php

namespace App\States\Order;

class Canceled extends OrderState
{
    protected static ?string $ability = 'cancel';
}

that mean it well look to ability name called cancel in OrderPolicy class.

also you can skip authorization by override the attribute $skip_authorization = true

keep in mind that the package also chick if you can transfer to the state using canTransitionTo(Cancel::class) that come with Spatie Package you can view it here

Customize Action Icon & Color

you can custom the state color and icon by overriding the attributes $color & $icon

<?php

namespace App\States\Contract;

class Canceled extends ContractState
{
    protected static ?string $color = 'danger';
    protected static ?string $icon = 'heroicon-o-cursor-arrow-rays';
}

for these attributes you have to follow Filament Doc about each one of them Icon Doc, Color Doc

Localization

you need to create states.php file to translate the state and the structure of each state should be as:

    'ClassName' => [
        'title' => '', //This is the name that well be displayed.
        'label' => '', //This is the action name that used in action button.
    ],

Display Current State

To display the current state in Table you can use StateClass::stateTextColumn() as column in table:

 public static function table(Table $table): Table
{
    return $table
        ->columns([
            //Other Columns
            ContractState::textColumn(),
            //Other Columns
        ]);
}

if you went to display the current state in Edit or Info List you can do so by adding state->display() method to header actions:

class ViewContract extends ViewRecord
{
    protected static string $resource = ContractResource::class;

    protected function getHeaderActions(): array
    {
        return [
            //Other Actions
            $this->record->state->display(),
            //Other Actions
        ];
    }
}

feel free to add it to any place that accept Filament\Actions\Action object.

to display the state as normal TextEntry inside InfoList you can do so by using StateClass::textEntry():

    public static function infolist(Infolist $infolist): Infolist
    {
        return $infolist
            ->schema([
                //...
                ContractState::textEntry(),
                //...
            ]);
    }

to include the select form filed use formSelect(static::getModel()) method:

public static function form(Form $form): Form
    {
        return $form
            ->schema([
                //....
                ContractState::formSelect(static::getModel()),
                //...
             ]);
    }

View actions in Table:

you can view the available actions 'depends on authorizations as states configuration':

    public static function table(Table $table): Table
    {
        return $table
            ->columns([])
            ->actions([
                    ...StateActionsService::make(static::getModel())->tableActions(),
            ]);
    }

tableActions() method well return an array of Filament\Tables\Actions\Action objects.

View actions in any page

you can view the available actions 'depends on authorizations as states configuration' in any resource page:

class ViewContract extends ViewRecord
{
    protected static string $resource = ContractResource::class;

    protected function getHeaderActions(): array
    {
        return [
            ...StateActionsService::make(Contract::class)
                ->actions(),
        ];
    }
}

actions() will return an array of Filament\Actions\Action objects, that mean you can use it any place that accept Action object.

View actions as group

you can view available actions in any resource page as group :

class ViewContract extends ViewRecord
{
    protected static string $resource = ContractResource::class;

    protected function getHeaderActions(): array
    {
        return [
            StateActionsService::make((static::$resource)::getModel(), 'importance')
                    ->excludeStates($this->record->importance)
                    ->actionsAsGroup($this->record)
        ];
    }
}

if you don't pass the record it will not display the current state.

Config ordering actions

if you went to display available actions in specific order you can do so by overriding $order attribute in each State.

class Approved extends PaymentState
{
    public static int $order = 1;    
}
class Rejected extends PaymentState
{
    public static int $order = 2;
}

action without confirmation modal

if you don't went the conformation modal you can set attribute $requires_confirmation to false in the state:

<?php

namespace App\States\Contract;

class Canceled extends ContractState
{
    protected static ?bool $requires_confirmation = false;
}

or change it in the base state class:

<?php

namespace App\States\Order;

use App\States\State;
use Spatie\ModelStates\Attributes;

abstract class OrderState extends State
{
    protected static ?bool $requires_confirmation = false;
}

this will stop confirmation modal to all states under this class.

Add State Filters To Table

you can include state filters to your table by using StateFilterService::make(Contract::class)->tableFilter()as:

public static function table(Table $table): Table
    {
        return $table
            ->columns([
                //
            ])
            ->filters([
                StateFilterService::make(static::getModel())
                    ->tableFilter(),
            ]);
    }

Testing

composer test

Changelog

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

Contributing

Please see CONTRIBUTING for details.

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.

abather/spatie-laravel-model-states-actions 适用场景与选型建议

abather/spatie-laravel-model-states-actions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 516 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 12 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 abather/spatie-laravel-model-states-actions 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 516
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-25