LaravelLaravel & VueJsVueJs

★ A package to assign statuses to Eloquent models

Uptime and SSL certificate monitoring doesn’t have to be boring, confusing or expensive. Together with my buddy Mattias Geniar I’m currently building Oh Dear: a webapp to keep an eye on the health of your websites. Register here to try it out.
(sponsored link)
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, only adding a single field won’t cut it. To handle these cases, our team has created a package called laravel-model-status.

After installing the package you must add the HasStatuses trait to all models that need to have statuses.

You can set a new status like this:

$model->setStatus('my first status');

A reason for the status change can be passed as a second argument.

$model->setStatus('my second status', 'I found a reason');

There are a few options to retrieve the latest status:

$model->status; // returns 'my second status'
$model->status(); // returns the latest instance of `SpatieModelStatusStatus`
$model->status()->reason; // returns 'I found a reason'

Of course you can also retrieve the whole status history of a model.

$model->statuses; // returns a collection of all statuses

The package provides a scope to retrieve all models that have a given status.

$allPendingModels = Model::currentStatus('pending');

To know more about the package, head over the the readme on GitHub. The principle author of package is our intern Thomas Verhelst. Be sure to also check out the other Laravel packages our team has previously made.

★ READ ALSO ★  5 Great Talks From VueConf 2017, The First Vue.js Conference

Source link

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.