Download the PHP package makeabledk/laravel-eloquent-status without Composer

On this page you can find all versions of the php package makeabledk/laravel-eloquent-status. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-eloquent-status

Laravel Eloquent Status

Latest Version on Packagist Build Status StyleCI

Check out the blog post explaining the concepts of this package:

https://medium.com/@rasmuscnielsen/an-eloquent-way-of-handling-model-state-c9aa372e9cb8


Most models has some sort of status or state to it. Few examples could be

Traditionally you may find yourself having a scopeAccepted and then additionally a ìsAccepted helper method to test a model-instance against a specific status.

This package offers a very handy way of dealing with statuses like these without cluttering you model.

When you've successfully setup this package you'll be able to achieve syntax like

``

``


Makeable is web- and mobile app agency located in Aarhus, Denmark.

Installation

You can install this package via composer:

Example usage

Given our Approval example from earlier we may have the following database fields:

Let's start out by creating a status class that holds our status definitions

Getting started

1. Create a status class

We will define all our valid statuses as public functions in a dedicated status class.

``

Notice how the statuses are defined just like regular scope functions. While this example is super simple, you have the full power of the Eloquent Query Builder at your disposal!

🔥 Tip: We recommend that your statuses has unambiguous definitions, meaning that a model can only pass one definition at a time. This will come in handy in the next few steps.

2. Apply trait on the model

Querying the database

Now we can query our database for approvals using the defined statuses:

Again, notice how this is very close to just calling a scope like we're used to: Approval::pending().

However there are som benefits to this new approach.

For instance this makes it convenient and safe to accept a raw status from a GET filter in your controller and return the result with no further validation or if-switches.

🔥 Checking model status

The real magic of the package!

We can actually use the same status definitions to check if a model instance adheres to a given status.

``

This sorcery is powered by our other package makeabledk/laravel-query-kit.

Note: Make sure to see the Limitations section of this readme.

Guessing model status

What if you wanted to know which status a model is from its attributes? Well you're in luck.

``

Now $approval->status would attempt resolve the approval status from your definitions.

Note: The status is guessed by checking each definition one-by-one until one passes. This is why you may consider unambiguous definitions.

Also you should be careful not to load relations in your definitions and generally consider a caching-strategy for large query-sets.

Furthermore see the Limitations section of this readme.

Binding a default status to a model

Rather than passing an instance of a status class each time you perform a check, you may bind a default status class to your model:

``

Now you may simply type name of the status

Other status classes than the default can still be used when passed explicitly.

You may bind the status classes in the boot function of your AppServiceProvider or create a separate service provider if you wish.

Limitations

This package is an abstraction on top of makeabledk/laravel-query-kit.

QueryKit provides a mocked version of the native QueryBuilder, allowing to run a scope function against a model instance.

This approach ensures great performance with no DB-queries needed, but introduces certain limitations.

While QueryKit supports most QueryBuilder syntaxes such as closures and nested queries, it does not support SQL language such as joins and selects. These limitation only applies to checkStatus() and guess() functions.

Check out the Limitations section in the makeabledk/laravel-query-kit documentation for more information.

Available methods on HasStatus

- scopeStatus($status)

- scopeStatusIn($statuses)

- checkStatus

- checkStatusIn

Testing

You can run the tests with:

Contributing

We are happy to receive pull requests for additional functionality. Please see CONTRIBUTING for details.

Credits

License

Attribution-ShareAlike 4.0 International. Please see License File for more information.


All versions of laravel-eloquent-status with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
illuminate/support Version ^10.0|^11.0
makeabledk/laravel-querykit Version ^3.0|^4.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package makeabledk/laravel-eloquent-status contains the following files

Loading the files please wait ....