Skip to content

a Laravel Package for adding stamp behaviors into laravel models.

License

Notifications You must be signed in to change notification settings

shetabit/stampable

Repository files navigation

Laravel Stampable

Software License Latest Version on Packagist StyleCI Maintainability Quality Score

This is a Laravel Package for adding stamp behaviors into laravel models. This package supports Laravel 5.2+.

List of contents

Install

Via Composer

$ composer require shetabit/stampable

How to use

Configure Migration

In your migration you must add timestamp field per each stamp.

// 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();

Configure Model

In your eloquent model add use HasStamps trait like the below.

<?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;

    //    
}

Define stamps

you can define stamps using protected stamps attribute the model

<?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',    
    ];

    //    
}

stamps must be in ['stampName' => 'databaseFieldName'] format.

Working with stamps

Model creates methods and local scopes for each stamp dynamically.

According to the latest example, now we have the below methods for each Category instance.

<?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

Change log

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

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

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