Skip to main content
Back to packages
1,054 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

Roles and Permissions in Laravel 13

14 lessons
57 min

Laravel 13 Eloquent: Expert Level

41 lessons
1 h 34 min

How to Structure Laravel 13 Projects

16 lessons
1 h 32 min read