The Laravel Inventory package helps track an inventory on any model with laravel.
The package offers the following function:
- Set a new inventory
- Return the current inventory
- Add to an inventory
- Subtract from an inventory
- Clear an inventory
- Return boolean if the model is in inventory
- Return boolean if the model is not in inventory
- Scopes
You can install the package via composer:
$ composer require caryley/laravel-inventoryMust 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 migrateAdd the HasInventory to the Model, the trait will enable inventory tracking.
...
use Caryley\LaravelInventory\HasInventory;
class Product extends Model
{
use HasInventory;
...
}$product = Product::first();
$product->setInventory(10); // $product->currentInventory()->quantity; (Will result in 10)
$product->currentInventory() //Return inventory instance
$product->addInventory(5); // $product->currentInventory()->quantity; (Will result in 15)
$product->subtractInventory(5); // $product->currentInventory()->quantity; (Will result in 10)
$product->inInventory(); // Retrun true
$product->clearInventory(); // $product->currentInventory(); (return null)
$product->notInInventory(); // Return true
--- Scopes ---
Product::InventoryIs(10)->get(); // Return all products with inventory of 10
Product::InventoryIs(10, '>=')->get(); // Return all products with inventory of 10 or greater
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]
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$product->setInventory(10); // $product->currentInventory()->quantity; (Will result in 10) | Not allowed to use negative numbers$product->currentInventory() //Return inventory instance$product->addInventory(); // Will increment inventory by 1
$product->addInventory(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->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(); // Will return a boolean 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 all 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 the first argument as quantity and the second argument as 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.