skore-labs/laravel-status

This package is abandoned and no longer maintained. No replacement package was suggested.

Laravel code-typed statuses for models

2.5.2 2022-07-15 20:37 UTC

README

⚠️ This package is gonna be deprecated in favour of this one: https://github.com/open-southeners/laravel-model-status

Laravel code-typed statuses for Eloquent models.

Status

packagist version tests StyleCI Codacy Badge Codacy Badge Scc Count Badge Scc Count Badge

Getting started

You can install the package via composer:

composer require skore-labs/laravel-status

Then you will need to publish the package config and migrations, so then you can modify and/or migrate the new statuses table:

php artisan vendor:publish --provider="SkoreLabs\LaravelStatus\ServiceProvider"

Setup models

Add statuses to your model by adding SkoreLabs\LaravelStatus\Traits\HasStatuses and the interface SkoreLabs\LaravelStatus\Contracts\Statusable so that it can pass some predefined events (see above), here's an example:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use SkoreLabs\LaravelStatus\Contracts\Statusable;
use SkoreLabs\LaravelStatus\Traits\HasStatuses;

class Post extends Model implements Statusable
{
    use HasStatuses;

    // Your model logic here...
}

Customize enum for status check (using spatie/enum package, check their documentation):

    /**
     * Get the statuses enum used for some utilities.
     * 
     * @return string|\Spatie\Enum\Enum
     */
    public static function statusesClass()
    {
        return \App\Statuses\PostStatuses::class;
    }

Note: This is not required, only if you DON'T have all your model status enum classes stored in App\Enums as ModelStatus.

Usage

Note: All methods doesn't have case sensitive on status names.

hasStatus

Check if model has status(es).

Note: It returns the current matched status name.

// Post has status Published
$post->hasStatus('published');

// Post has status Published or Was Published
$post->hasStatus(['published', 'was published']);

setStatus

Set status or mutate status only if the previous status match the key.

// Set post status to Was Published
$post->setStatus('was published');

// Change if post has status Published to Was Published.
$post->setStatus(['published' => 'was published']);

You can also use the attribute to set a status:

$post->status = 'was published';

// Better use status method for this
if ($post->hasStatus('published')) {
    $post->status = 'was published';
}

// When save it check and attach the status
$post->save();

setStatusWhen

You can also do the same with setStatusWhen method like the example above with setStatus.

// Change if post has status Published to Was Published.
$post->setStatusWhen('published', 'was published');

status

If a parameter is provided, it acts as an alias of hasStatus.

If an associative array is provided, it acts as an alias of setStatus.

Otherwise, it will just retrieve the relationship as $post->status or $post->status()->first()

Also you can filter by scope:

Post::status('published');
Post::where('user_id', Auth::id())->status('published');

statuses

Get all the possible model statuses.

Post::statuses();

// You can use Status model as well
Status::getFrom(Post::class);
// Also specify value to return like '->value('id')'
Status::getFrom(Post::class, 'id');
// Or return the object with columns like '->first(['id', 'name'])'
Status::getFrom(Post::class, ['id', 'name']);

getDefaultStatus

Get the model's default status.

// Default status for post is Published, so it returns Published
Post::getDefaultStatus();

// You can use Status model query scope as well
Status::query()->defaultFrom(Post::class)->first();

Support

This and all of our Laravel packages follows as much as possibly can the LTS support of Laravel.

Read more: https://laravel.com/docs/master/releases#support-policy

Credits