承接 thefeqy/laravel-model-status 相关项目开发

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

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

thefeqy/laravel-model-status

Composer 安装命令:

composer require thefeqy/laravel-model-status

包简介

A Laravel package to automate adding configurable status columns to models and migrations, and enforce user activity through middleware.

README 文档

README

Laravel Model Status

Latest Version on Packagist Total Downloads PHP Version GitHub Workflow Status License

A Laravel package that simplifies status management for Eloquent models.

Table of Contents

Features

  • HasActiveScope Trait: Automatically filters models with active status.
  • Admin Bypass: Admin users can see all models, including inactive ones.
  • Helper Methods: $model->status->isActive() and $model->status->isInactive().
  • Dynamic Configuration: Define custom statuses & column names via .env.
  • Installation Command: php artisan model-status:install for easy setup.
  • PHP 8.3 Support.

Installation

You can install the package via Composer:

composer require thefeqy/laravel-model-status

Step 1: Run the Installation Command

php artisan model-status:install

This will:

  • Publish the config file (config/model-status.php).
  • Set up required environment variables in .env and .env.example.
  • Ensure your project is ready to use the package.

Configuration

You can customize the package settings in:

config/model-status.php

return [
    'column_name' => env('MODEL_STATUS_COLUMN', 'status'),
    'default_value' => env('MODEL_STATUS_ACTIVE', 'active'),
    'inactive_value' => env('MODEL_STATUS_INACTIVE', 'inactive'),

    'admin_detector' => function () {
        return auth()->check() && auth()->user()->is_admin;
    },
];

Using .env Variables

Instead of modifying config/model-status.php, you can override values in .env:

.env

MODEL_STATUS_COLUMN=state
MODEL_STATUS_ACTIVE=enabled
MODEL_STATUS_INACTIVE=disabled

Now, the package will automatically adapt to your setup.

Usage

Using the HasActiveScope Trait

To enable status management in a model:

use Thefeqy\ModelStatus\Traits\HasActiveScope;

class Product extends Model
{
    use HasActiveScope;

    protected $fillable = ['name'];
}

Now, inactive models are automatically excluded from queries.

Querying Models

Get Active Models (Default Behavior)

$activeProducts = Product::all(); // Returns only active products

Get All Models (Including Inactive)

$allProducts = Product::inActive()->get();

Manually Activating / Deactivating a Model

$product = Product::find(1);

$product->activate(); // Set status to "active"
$product->deactivate(); // Set status to "inactive"

Checking a Model's Status

if ($product->status->isActive()) {
    echo "Product is active!";
}

if ($product->status->isInactive()) {
    echo "Product is inactive!";
}

Using Status Casting

To ensure status is always cast to a Status object, use the StatusCast class.

Apply Status Casting in Models

use Thefeqy\ModelStatus\Casts\StatusCast;

class Product extends Model
{
    use HasActiveScope;

    protected $fillable = ['name', 'status', 'category_id'];

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

Example Usage

$product = Product::find(1);

if ($product->status->isActive()) {
    echo "Product is active!";
}

Now, $product->status is an instance of Status, allowing method calls like isActive() and isInactive().

Cascade Deactivation

If a model is deactivated, its related models can also be automatically deactivated.

Example

class Category extends Model
{
    use HasActiveScope;

    protected $fillable = ['name', 'status'];

    protected array $cascadeDeactivate = ['products'];

    public function products()
    {
        return $this->hasMany(Product::class);
    }
}

Now, deactivating a category will also deactivate all products under it:

$category = Category::find(1);
$category->deactivate();

Using the Middleware

The package includes the EnsureAuthenticatedUserIsActive middleware, which enforces that only users with an active status can access certain routes.

Add Middleware to Routes

Instead of registering a string alias for the middleware, you can reference it by class name in your route definition:

use Illuminate\Support\Facades\Route;
use Thefeqy\ModelStatus\Middleware\EnsureAuthenticatedUserIsActive;

Route::middleware(['auth', EnsureAuthenticatedUserIsActive::class])->group(function () {
    Route::get('/dashboard', function () {
        return 'Welcome to your dashboard!';
    });
});

Admin Bypass for Active Scope

By default, admin users can see inactive models.

This behavior is controlled in config/model-status.php:

'admin_detector' => function () {
    return auth()->check() && auth()->user()->is_admin;
},

Advanced Configuration

If you need a different column name or status values, update .env:

MODEL_STATUS_COLUMN=state
MODEL_STATUS_ACTIVE=enabled
MODEL_STATUS_INACTIVE=disabled

Now, models will use:

$table->string('state', 10)->default('enabled');

instead of:

$table->string('status', 10)->default('active');

Testing

Run tests using Pest PHP:

composer test
or
vendor/bin/phpunit

Security

If you discover a security vulnerability, please report it via email: 📩 thefeqy@gmail.com

Contributing

Want to improve this package? Check out CONTRIBUTING for contribution guidelines.

License

This package is open-source software licensed under the MIT License.

thefeqy/laravel-model-status 适用场景与选型建议

thefeqy/laravel-model-status 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 522 次下载、GitHub Stars 达 50, 最近一次更新时间为 2025 年 01 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 thefeqy/laravel-model-status 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 522
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 50
  • 点击次数: 11
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-17