⚠️ 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.
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"
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
.
Note: All methods doesn't have case sensitive on status names.
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']);
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();
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');
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');
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']);
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();
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
- Ruben Robles (@d8vjork)
- Skore (https://www.getskore.com/)
- Spatie for the Enum package (https://spatie.be/)
- And all the contributors