PHP code example of open-southeners / laravel-model-status
1. Go to this page and download the library: Download open-southeners/laravel-model-status library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
open-southeners / laravel-model-status example snippets
use OpenSoutheners\LaravelModelStatus\ModelStatus;
enum PostStatus: int implements ModelStatus
{
case Draft = 1;
case Published = 2;
case Hidden = 3;
}
use OpenSoutheners\LaravelModelStatus\Attributes\ModelStatuses;
use OpenSoutheners\LaravelModelStatus\HasStatuses;
use OpenSoutheners\LaravelModelStatus\Statusable;
// Remember to replace PostStatus::class with whatever the enum you are using
// Also second option is a boolean that enable/disable events
#[ModelStatuses(PostStatus::class, true)]
class Post extends Model implements Statusable
{
use HasStatuses;
}
$post = new Post();
// Set status to post instance
$post->setStatus(PostStatus::Published);
// Set status to post instance and persist to DB
$post->setStatus(PostStatus::Published, true);
// Set status to post instance only when current status is "Draft"
$post->setStatusWhen(PostStatus::Draft, PostStatus::Published);
// Set status to post instance and persist to DB only when current status is "Draft"
$post->setStatusWhen(PostStatus::Draft, PostStatus::Published, true);
// Check current status is "Published"
$post->hasStatus(PostStatus::Published);
// Check current status is "Published" as string (type sensitive)
$post->hasStatus('Published');
// Whenever you save the model updates and don't want to trigger events
Post::withoutStatusEvents(fn () => $post->setStatus(PostStatus::Published));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.