shetabit/stampable

将戳记行为添加到Laravel模型中。

v1.0.1 2019-06-21 18:29 UTC

This package is auto-updated.

Last update: 2024-08-29 05:00:39 UTC


README

Laravel Stampable

Software License Latest Version on Packagist StyleCI Maintainability Quality Score

这是一个用于将戳记行为添加到Laravel模型的Laravel包。此包支持 Laravel 5.2+

内容列表

安装

通过Composer

$ composer require shetabit/stampable

如何使用

配置迁移

在你的迁移中,你必须为每个戳记添加一个 timestamp 字段。

// In migration, you must add published_at field like the below if you want to use it as a stamp.
$table->timestamp('published_at')->nullable();

配置模型

在你的Eloquent模型中添加 use HasStamps 特性,如下所示。

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Shetabit\Stampable\Contracts\Stampable;
use Shetabit\Stampable\Traits\HasStamps;

class Category extends Model implements Stampable
{
    use HasStamps;

    //    
}

定义戳记

你可以使用模型中的 protected stamps 属性来定义戳记

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Shetabit\Stampable\Contracts\Stampable;
use Shetabit\Stampable\Traits\HasStamps;

class Category extends Model implements Stampable
{
    use HasStamps;

    protected $stamps = [
        'published' => 'published_at',    
    ];

    //    
}

戳记必须以 ['stampName' => 'databaseFieldName'] 格式。

与戳记一起工作

模型将动态创建每个戳记的方法和本地范围。

根据最新的示例,现在每个 Category 实例都有以下方法。

<?php

/**
 * notice that be have all of this methods and scopes for each stamps.
 * the name of methods will be similar to the stamp's name.
**/

// methods:
$category->markAsPublished(); // press published stamp on this category!
$category->markAsUnpublished(); // Remove stamp mark from this category.

$category->isPublished(); // Determines if this category is published.
$category->isnUnpublished(); // Determinces if this category is Unpublished.

// scopes: you can use scopes to filter your data using stamp status.
Category::published()->get(); // retrieve published datas
Category::unpublished()->get(); // retrieve unpublished datas

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

请参阅 CONTRIBUTINGCONDUCT 了解详细信息。

安全

如果您发现任何安全问题,请通过电子邮件 khanzadimahdi@gmail.com 而不是使用问题跟踪器。

鸣谢

许可

MIT许可证(MIT)。有关更多信息,请参阅 许可文件