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
composer require caryley/laravel-inventoryPublish 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 migrateAdd the HasInventory trait to the model.
...
use Caryley\LaravelInventory\HasInventory;
class Product extends Model
{
use HasInventory;
...
}$product->hasValidInventory(); // Determine if the model has a valid inventory.$product->setInventory(10); // $product->currentInventory()->quantity; (Will result in 10) | Not allowed to use negative numbers.$product->currentInventory() //Return inventory instance if one exists, if not it will return null.$product->addInventory(); // Will increment inventory by 1.
$product->addInventory(10); // Will increment inventory by 10.$product->incrementInventory(10); // Will increment inventory by 10.$product->subtractInventory(5); // Will subtract 5 from current inventory.
$product->subtractInventory(-5); // Will subtract 5 from current inventory.$product->decrementInventory(5); // Will subtract 5 from current inventory.
$product->decrementInventory(-5); // Will subtract 5 from current inventory.$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.$product->notInInventory(); // Determine if model inventory is less than 0.$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.- 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- 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,3Please see the changelog for more information on what has changed recently.
composer testPlease see contributing.md for details and a todolist.
If you discover any security related issues, please email author email instead of using the issue tracker.
Laravel-Inventory draws inspiration from spatie/laravel-model-status & appstract/laravel-stock (even though it doesn't rely on any of them).
license. Please see the license file for more information.