定制 uutkukorkmaz/laravel-statuses 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

uutkukorkmaz/laravel-statuses

最新稳定版本:v0.1.1

Composer 安装命令:

composer require uutkukorkmaz/laravel-statuses

包简介

A package that makes managing the model statuses easier

README 文档

README

Laravel Statuses

Laravel Statuses

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

Laravel Statuses is a package that makes managing the model statuses easier. It provides a trait that you can use in your models to add statuses to them.

Installation

You can install the package via composer:

composer require uutkukorkmaz/laravel-statuses

You can publish the config file with:

php artisan vendor:publish --tag="statuses-config"

This is the contents of the published config file:

return [

    'namespace' => 'Enums\\Statuses',

    'allow_sequential' => true,

];

Usage

Basic Usage

To create a new status, you can use the status:generate command. This will generate an Enum in your project's app/Enums directory.

php artisan status:generate OrderStatus

Defining Cases

You can define cases for your status by adding constants to your Enum class. The name of the constant will be used as the case name, and the value will be used as the case value.

php artisan status:generate OrderStatus --cases Pending,Approved,Processing,Shipped,Delivered

The result of the above command will be:

<?php

namespace App\Enums\Statuses;

enum OrderStatus: string
{
    case PENDING = 'pending';
    case APPROVED = 'approved';
    case PROCESSING = 'processing';
    case SHIPPED = 'shipped';
    case DELIVERED = 'delivered';
}

Sequential Statuses

This type of statuses are used when the next status is always the next case in the enum. For example, if you have a status for a user's account, the next status will always be the next case in the enum. For example, if the current status is PENDING, the next status will be APPROVED.

php artisan status:generate AccountStatus --sequential --cases Pending,Approved

The result of the above command will be:

<?php

namespace App\Enums\Statuses;

enum AccountStatus: string
{

    case PENDING = 'pending';
    case APPROVED = 'approved';


    public function next(): self
    {
        return match ($this) {
            self::PENDING => self::APPROVED,
            default => throw new \LogicException('Invalid status'),
        };
    }

    public function previous(): self
    {
        return match($this) {
            self::APPROVED => self::PENDING,
            default => throw new \LogicException('Invalid status'),
        };
    }
}

Using Statuses with Models

To attach a status to a model, you can use the HasStatus trait. This trait will add a status field to your model.

use Uutkukorkmaz\LaravelStatuses\Concerns\HasStatus;

class Order extends Model
{
    use HasStatus;
    
    // ...
}

You can also attach status to a model automatically when creating the status with following command:

php artisan status:generate OrderStatus --model=Order --sequential --cases Pending,Approved,Processing,Shipped,Delivered

Please note that before the calling the command above you should have the model and the model must have protected $casts line.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
// ...

class Order extends Model
{
    use HasStatus;
    
    // ...
    
    protected $casts = [
      'status' => \App\Enums\Statuses\OrderStatus::class,
    ];
    
    // ...
}

Testing

composer test

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Credits

License

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固