1. Go to this page and download the library: Download spatie/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/ */
spatie / laravel-model-status example snippets
// set a status
$model->setStatus('pending', 'needs verification');
// set another status
$model->setStatus('accepted');
// specify a reason
$model->setStatus('rejected', 'My rejection reason');
// get the current status
$model->status(); // returns an instance of \Spatie\ModelStatus\Status
// get the previous status
$latestPendingStatus = $model->latestStatus('pending');
$latestPendingStatus->reason; // returns 'needs verification'
return [
/*
* The class name of the status model that holds all statuses.
*
* The model must be or extend `Spatie\ModelStatus\Status`.
*/
'status_model' => Spatie\ModelStatus\Status::class,
/*
* The name of the column which holds the ID of the model related to the statuses.
*
* You can change this value if you have set a different name in the migration for the statuses table.
*/
'model_primary_key_attribute' => 'model_id',
];
use Spatie\ModelStatus\HasStatuses;
class YourEloquentModel extends Model
{
use HasStatuses;
}
$model->status; // returns a string with the name of the latest status
$model->status(); // returns the latest instance of `Spatie\ModelStatus\Status`
$model->latestStatus(); // equivalent to `$model->status()`
$model->latestStatus('pending'); // returns an instance of `Spatie\ModelStatus\Status` that has the name `pending`
$statusNames = $model->getStatusNames(); // returns a collection of all available status names.
public function isValidStatus(string $name, ?string $reason = null): bool
{
...
if (! $condition) {
return false;
}
return true;
}
$model->forceSetStatus('invalid-status-name');
$model->hasEverHadStatus('status 1');
$model->hasNeverHadStatus('status 1');
$model->deleteStatus('status 1');
$model->deleteStatus(['status 1', 'status 2']);
namespace Spatie\ModelStatus\Events;
use Illuminate\Database\Eloquent\Model;
use Spatie\ModelStatus\Status;
class StatusUpdated
{
/** @var \Spatie\ModelStatus\Status|null */
public $oldStatus;
/** @var \Spatie\ModelStatus\Status */
public $newStatus;
/** @var \Illuminate\Database\Eloquent\Model */
public $model;
public function __construct(?Status $oldStatus, Status $newStatus, Model $model)
{
$this->oldStatus = $oldStatus;
$this->newStatus = $newStatus;
$this->model = $model;
}
}