abather/spatie-laravel-model-states-actions

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

1.0.3 2024-12-25 10:48 UTC

This package is auto-updated.

Last update: 2024-12-25 10:49:42 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status 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

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.

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.

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.

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.