Keep Track of Model Inventory with Laravel Stock

Packages

March 25th, 2020

laravel-stock.png

Laravel Stock is a package by Appstract for keeping track of inventory counts on models:

Keep stock for Eloquent models. This package will track stock mutations for your models. You can increase, decrease, clear, and set stock. It’s also possible to check if a model is in stock (on a certain date/time).

For example, let’s say you have a Book model with which you need to keep track of stock:

1use Appstract\Stock\HasStock;
2 
3class Book extends Model
4{
5 use HasStock;
6}

When a customer places an order for a book, you can change the stock counts:

1$book->increaseStock(10);
2$book->decreaseStock(10);
3 
4// Change stock positively or negatively with one method
5$book->mutateStock(10);
6$book->mutateStock(-10);

Next, in your UI you could check to see if a product is in stock:

1$book->inStock();
2// See if you have at least 10 of the same book in stock
3$book->inStock(10);

Finally, you can clear stock out:

1$book->clearStock();
2 
3// Clear stock and then set a new value
4$book->clearStock(10);

You can learn more about this package, get full installation instructions, and view the source code on GitHub at appstract/laravel-stock.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.