Skip to content

basarozcan/Laravel-Inventory

Repository files navigation

Laravel Inventory

GitHub Workflow Status Latest Version on Packagist Total Downloads StyleCI

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

Installation

You can install the package via composer:

$ composer require caryley/laravel-inventory

Must 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 to the Model, the trait will enable inventory tracking.

...
use Caryley\LaravelInventory\HasInventory;

class Product extends Model
{
    use HasInventory;

    ...
}

Functions

$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

SetInventory()

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

currentInventory()

$product->currentInventory() //Return inventory instance

addInventory()

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

$product->addInventory(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

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(); // Will return a boolean 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 all inventory for the model and will set new inventory of 10

InventoryIs() Scope

  • 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() Scope

  • 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,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.

About

Create an inventory for laravel models.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages