Download the PHP package nzesalem/lastus without Composer
On this page you can find all versions of the php package nzesalem/lastus. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package lastus
Lastus
Easy status addition and management for your Eloquent models in Laravel.
What is lastus/status?
Consider you are building a forum app, you would typically have a User
model/class. A User
can be in different states or have different statuses at different points in time. For example when a user first registers for the forum you may want his/her status to be unverified
indicating that the user have not verified his/her email. When the user verifies his/her email, he/she may become active
. If the user violates one or more of the forum rules, he/she may become suspended
, and so on.
Lastus package for Laravel 5 aims to handle all of this for you automatically, with minimal configuration.
-
Install the package via Composer:
The package will automatically register itself with Laravel 5.5 and later.
Database migrations
Your database tables should have a status
column defined as the tinyInteger
type. Lastus automatically adds this column to your users
table (if it doesn't already exist) when you run the php artisan migrate
command after adding Lastus to your project.
You should manually generate migrations for other of your tables as needed. Below is an example of how you would add the status
column to a posts
table.
First run:
Then edit the generated migration file like so (usually located at database/migrations
):
And finally run:
Updating your Eloquent Models
Your models should use the LastusTrait
and define a STATUSES
array constant.
The keys of the STATUSES
array should be in all caps and multiple words should be separated with underscore. Ideally it should follow the same naming rules and convention as PHP constants.
The values of the STATUSES
array can be any number within the TINYINT
range.
And that's it ...
Usage
When saving models, just set its status property to one of the keys you have defined above, but in all lower case, multiple words should be separated by a hyphen. E.g PENDING_APPROVAL
becomes pending-approval
. This is the format you will use most of the time when working with statuses.
Retrieving a model status:
You can get the status code whenever you need it. For example, status key strings will not work if you try to perform raw queries with them. So, in those cases you need the status codes instead:
Getting all the defined statuses for a given model is also easy as the snippet below. We get all the defined statuses for the User
model and display them in a select element:
Or you can use the Lastus
facade:
Blade templates
You can use the @status()
blade directive to control the visibility of elements based on the status of the currently logged in user:
Middleware
You can use a middleware to filter routes and route groups by status:
License
Lastus is released under the MIT License.