mendela92 / laravel-stock
Keep stock for Eloquent models
Fund package maintenance!
Patreon
Requires
- php: ^7.2|^8.0
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-06-14 12:00:35 UTC
README
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). But also watches the level of stock of a model and sends a notification via Email
This package was originally a fork form laravel-stock
Functionality
- Increase stocks
- Decrease stocks
- Clear stocks
- Set stocks
- Check if model has stock or not (on a certain date/time).
- Send notification when a pre-defined stock level is reached
Installation
You can install the package via composer:
composer require mendela92/laravel-stock
By running php artisan vendor:publish --provider="Mendela92\Stock\StockServiceProvider"
, it will publish migrations and config file.
The configuration file looks this:
<?php use Mendela92\Stock\Notifications\LowStockLevelNotification; return [ /* |-------------------------------------------------------------------------- | Default table name |-------------------------------------------------------------------------- | | Table name to use to store mutations. | */ 'table' => 'stocks', /* |-------------------------------------------------------------------------- | Default notification stock level |-------------------------------------------------------------------------- | | Default stock alert configuration values | */ 'alert' => [ 'notification' => env("STOCK_NOTIFICATION", true), 'at' => env("NOTIFICATION_STOCK_LEVEL", 10), 'to' => ['email@example.com'], 'model' => LowStockLevelNotification::class, ], ];
Run php artisan migrate
to migrate the table. There will now be a stocks
table in your database.
Usage
Adding the HasStock
trait will enable stock functionality on the Model.
use Mendela92\Stock\HasStock; class Book extends Model { use HasStock; ... public function getStockAlertAt(): mixed { return config('stock.alert.at', 10); // Change the value of the level of stock before being notification is sent. } public function getStockAlertTo(): mixed { // Array of emails that the notifications will be sent to. } public function getStockNotificationStatus(): mixed { // Conditioning notification of status for each model instance } }
Basic mutations
$book->increaseStock(10); $book->decreaseStock(10); $book->mutateStock(10); $book->mutateStock(-10);
Clearing stock
It's also possible to clear the stock and directly setting a new value.
$book->clearStock(); $book->clearStock(10);
Setting stock
It is possible to set stock. This will create a new mutation with the difference between the old and new value.
$book->setStock(10);
Check if model is in stock
It's also possible to check if a product is in stock (with a minimal value).
$book->inStock(); $book->inStock(10);
Current stock
Get the current stock value (on a certain date).
$book->stock; $book->stock(Carbon::now()->subDays(10));
Stock arguments
Add a description and/or reference model to de StockMutation.
$book->increaseStock(10, [ 'description' => 'This is a description', 'reference' => $otherModel, ]);
Query Scopes
It is also possible to query based on stock.
Book::whereInStock()->get(); Book::whereOutOfStock()->get();
Testing
composer test
Contributing
Contributions are welcome, thanks to y'all :)
License
The MIT License (MIT). Please see License File for more information.