caryley/laravel-inventory

Inventory feature for laravel models.

3.0.0 2023-01-25 18:12 UTC

This package is auto-updated.

Last update: 2024-09-05 21:09:54 UTC


README

GitHub Workflow Status Latest Version on Packagist Total Downloads StyleCI

The Laravel Inventory package helps track an inventory on any Laravel model.


The package offers the following functionality:

  • Create and set a new inventory
  • Retrieve the current inventory
  • Manage inventory quantity
  • Clear an inventory
  • Determine if the model is in inventory or not.
  • Determine if the model has a valid inventory
  • Query scopes for inventoriable model

Installation

composer require caryley/laravel-inventory

Publish the migration with:

php artisan vendor:publish --provider="Caryley\LaravelInventory\LaravelInventoryServiceProvider" --tag="migrations"

Or optionaly publish togther with config file:

php artisan vendor:publish --provider="Caryley\LaravelInventory\LaravelInventoryServiceProvider"

Migrate inventories table:

php artisan migrate

Usage

Add the HasInventory trait to the model.

...
use Caryley\LaravelInventory\HasInventory;

class Product extends Model
{
    use HasInventory;

    ...
}

hasValidInventory()

$product->hasValidInventory(); // Determine if the model has a valid inventory.

setInventory()

$product->setInventory(10); // $product->currentInventory()->quantity; (Will result in 10) | Not allowed to use negative numbers.

currentInventory()

$product->currentInventory() //Return inventory instance if one exists, if not it will return null.

addInventory()

$product->addInventory(); // Will increment inventory by 1.

$product->addInventory(10); // Will increment inventory by 10.

incrementInventory()

$product->incrementInventory(10); // Will increment inventory by 10.

subtractInventory()

$product->subtractInventory(5); // Will subtract 5 from current inventory.

$product->subtractInventory(-5); // Will subtract 5 from current inventory.

decrementInventory()

$product->decrementInventory(5); // Will subtract 5 from current inventory.

$product->decrementInventory(-5); // Will subtract 5 from current inventory.

inInventory()

$product->inInventory(); // Will return a boolean if model inventory greater than 0.

$product->inInventory(10); // Will return a boolean if model inventory greater than 10.

notInInventory()

$product->notInInventory(); // Determine if model inventory is less than 0.

clearInventory()

$product->clearInventory(); // Will clear all inventory for the model **Will delete all records, not only last record.

$product->clearInventory(10); // Will clear the inventory for the model and will set new inventory of 10.

Scopes

InventoryIs()

  • The scope accepts the first argument as quantity, the second argument as the operator, and the third argument as a model id or array of ids.
Product::InventoryIs(10)->get(); // Return all products with inventory of 10.

Product::InventoryIs(10, '<=')->get(); // Return all products with inventory of 10 or less.

Product::InventoryIs(10, '>=', [1,2,3])->get(); // Return all products with inventory of 10 or greater where product id is 1,2,3

InventoryIsNot()

  • The scope accepts a first argument of a quantity and a second argument of a model id or array of ids
Proudct::InventoryIsNot(10)->get(); // Return all products where inventory is not 10

Proudct::InventoryIsNot(10, [1,2,3])->get(); // Return all products where inventory is not 10 where product id is 1,2,3

Change log

Please see the changelog for more information on what has changed recently.

Testing

composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email author email instead of using the issue tracker.

Credits

Acknowledgements

Laravel-Inventory draws inspiration from spatie/laravel-model-status & appstract/laravel-stock (even though it doesn't rely on any of them).

License

license. Please see the license file for more information.