Installation
composer require baks-dev/materials-stocks
Ensure your Laravel project meets the PHP 8.4+ requirement and has baks-dev/core (≥7.4) installed.
Publish Config & Migrations
php artisan vendor:publish --provider="BaksDev\MaterialsStocks\MaterialsStocksServiceProvider" --tag="config"
php artisan vendor:publish --provider="BaksDev\MaterialsStocks\MaterialsStocksServiceProvider" --tag="migrations"
Run migrations:
php artisan migrate
First Use Case: Register a Material
use BaksDev\MaterialsStocks\Facades\MaterialsStocks;
// Register a new material type (e.g., "Wood")
MaterialsStocks::material()->create([
'name' => 'Wood',
'code' => 'WD',
'unit' => 'kg',
]);
Key Facades
MaterialsStocks::material() → Material CRUDMaterialsStocks::stock() → Stock movement operationsMaterialsStocks::report() → Generate stock reports$material = MaterialsStocks::material()->create([
'name' => 'Steel',
'code' => 'ST',
'unit' => 'ton',
'description' => 'Carbon steel',
]);
import method with a SplFileInfo object:
$file = new SplFileInfo(storage_path('app/materials.csv'));
MaterialsStocks::material()->import($file);
MaterialsStocks::stock()->incoming([
'material_id' => 1, // Wood
'quantity' => 500,
'unit_cost' => 10.50,
'source' => 'Supplier X',
'notes' => 'Delivery #123',
]);
MaterialsStocks::stock()->outgoing([
'material_id' => 1,
'quantity' => 100,
'destination' => 'Production Line A',
'reference_id' => 42, // Related order ID
]);
MaterialsStocks::stock()->adjust([
'material_id' => 1,
'quantity' => -50, // Negative for deductions
'reason' => 'Wastage',
]);
$report = MaterialsStocks::report()->getStockReport(
materialIds: [1, 2], // Optional filter
startDate: now()->subMonth(),
endDate: now()
);
$report = MaterialsStocks::report()->getStockReport();
return Excel::download(new StockReportExport($report), 'stock_report.xlsx');
StockCreated) in your EventServiceProvider:
protected $listen = [
\BaksDev\MaterialsStocks\Events\StockCreated::class => [
\App\Listeners\UpdateInventory::class,
],
];
use BaksDev\MaterialsStocks\Http\Resources\MaterialResource;
class CustomMaterialResource extends MaterialResource {
public function toArray($request) {
$array = parent::toArray($request);
$array['custom_field'] = 'value';
return $array;
}
}
Extend the default validator by creating a custom rule:
use BaksDev\MaterialsStocks\Rules\MaterialRule;
class CustomMaterialRule extends MaterialRule {
public function passes($attribute, $value) {
// Add custom logic (e.g., check against another table)
return parent::passes($attribute, $value);
}
}
Override default stock behavior (e.g., enforce minimum stock levels):
use BaksDev\MaterialsStocks\Contracts\StockPolicy;
class MinimumStockPolicy implements StockPolicy {
public function beforeOutgoing($material, $quantity) {
if ($material->current_stock - $quantity < 100) {
throw new \Exception("Minimum stock level violated!");
}
}
}
Register the policy in config/materials-stocks.php:
'policies' => [
\App\Policies\MinimumStockPolicy::class,
],
Use the warehouse_id field in stock operations:
MaterialsStocks::stock()->incoming([
'material_id' => 1,
'warehouse_id' => 2, // Secondary warehouse
'quantity' => 200,
]);
PHP Version Mismatch
php.ini and Laravel environment match.Missing Core Dependency
baks-dev/core (≥7.4) is mandatory. Install it first:
composer require baks-dev/core:^7.4
Class 'BaksDev\Core\BaseModel' not found
→ Verify baks-dev/core is loaded in config/app.php under providers.Migration Conflicts
materials or stocks tables, merge migrations manually before running:
php artisan migrate --step
php artisan vendor:publish --tag=migrations to inspect the raw migration files first.Facade Not Found
config/app.php:
BaksDev\MaterialsStocks\MaterialsStocksServiceProvider::class,
php artisan package:discover to refresh providers.Stock Quantity Precision
string or use bcmath:
$total = (string) $stock->quantity * (float) $stock->unit_cost;
Enable Query Logging
Add to AppServiceProvider:
public function boot() {
if (app()->environment('local')) {
\DB::enableQueryLog();
}
}
Check logs after stock operations:
dd(\DB::getQueryLog());
Test Stock Events
Use the listeners config to log events:
'events' => [
'StockCreated' => \App\Listeners\LogStockEvent::class,
],
Validate Material Codes
The code field is unique. Use:
MaterialsStocks::material()->updateOrCreate(
['code' => 'WD'],
['name' => 'Wood Updated']
);
Custom Material Attributes
Extend the Material model:
use BaksDev\MaterialsStocks\Models\Material;
class CustomMaterial extends Material {
protected $casts = [
'is_perishable' => 'boolean',
];
}
Update the config to use your model:
'models' => [
'material' => \App\Models\CustomMaterial::class,
],
Stock Movement Notifications Publish the event listener template:
php artisan vendor:publish --tag=materials-stocks-events
Extend the default listener:
class CustomStockListener {
public function handle(StockCreated $event) {
// Send Slack/Email notification
}
}
API Rate Limiting
Add middleware to the StockController:
Route::middleware(['throttle:10,1'])->group(function () {
Route::apiResource('stocks', \BaksDev\MaterialsStocks\Http\Controllers\StockController::class);
});
Localization Override translations:
php artisan vendor:publish --tag=materials-stocks-lang
How can I help you explore Laravel packages today?