Skip to main content
Back to packages
1,053 GitHub stars

spatie/laravel-model-status

View on GitHub

Description

Easily add statuses to your models

Imagine you want to have an Eloquent model hold a status. It's easily solved by just adding a status field to that model and be done with it. But in case you need a history of status changes or need to store some extra info on why a status changed, just adding a single field won't cut it.

This package provides a HasStatuses trait that, once installed on a model, allows you to do things like this:

// set a status
$model->setStatus('pending', 'needs verification');
 
// set a status using an enum
$model->setStatus(UserStatus::pending);
 
// 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'

Related Content on Laravel Daily

Video

Recent Courses on Laravel Daily

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.