baks-dev/materials-catalog
Laravel/PHP module for managing a raw materials catalog. Install via Composer, compatible with PHP 8.4+. Includes PHPUnit test group for materials-catalog. MIT licensed.
## Getting Started
### Minimal Setup
1. **Install Dependencies**
```bash
composer require baks-dev/materials-category baks-dev/materials-catalog baks-dev/core:^7.4
baks-dev/core is a hard dependency; ensure Laravel compatibility.Publish Configurations
php artisan vendor:publish --provider="BaksDev\MaterialsCatalog\MaterialsCatalogServiceProvider"
config/materials-catalog.php exists and update database settings.Run Migrations
php artisan migrate
First Use Case: List Materials
use BaksDev\MaterialsCatalog\Material;
$materials = Material::query()
->with('category', 'attributes')
->where('is_active', true)
->paginate(20);
$materials = app(\BaksDev\MaterialsCatalog\Service::class)->findAll();
Basic Search
$results = Material::search('steel')
->where('supplier_id', auth()->user()->supplier->id)
->get();
$results = app(\BaksDev\MaterialsCatalog\SearchService::class)->search('steel');
// app/Repositories/MaterialRepository.php
namespace App\Repositories;
use BaksDev\MaterialsCatalog\Material as BaseMaterial;
use Illuminate\Database\Eloquent\Model;
class MaterialRepository
{
public function create(array $data): Model
{
return BaseMaterial::create($data);
}
}
bind() to resolve the repository:
$this->app->bind(
\App\Repositories\MaterialRepository::class,
\BaksDev\MaterialsCatalog\MaterialRepository::class
);
// Example: Filter materials by density range
$materials = Material::query()
->whereHas('attributes', function ($query) {
$query->where('name', 'density')
->where('value', '>=', 7.8); // g/cm³ for steel
})
->get();
$filter = app(\BaksDev\MaterialsCatalog\FilterBuilder::class)
->add('density', '>=', 7.8)
->build();
$materials = app(\BaksDev\MaterialsCatalog\Service::class)->filter($filter);
// app/Models/Material.php
public function supplier()
{
return $this->belongsTo(Supplier::class);
}
class ExtendedMaterial extends \BaksDev\MaterialsCatalog\Material
{
public function supplier()
{
return $this->belongsTo(Supplier::class, 'supplier_id');
}
}
// app/Http/Resources/MaterialResource.php
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'category' => CategoryResource::make($this->category),
'attributes' => AttributeResource::collection($this->attributes),
'supplier' => SupplierResource::make($this->supplier),
];
}
Route::apiResource('materials', MaterialController::class);
// app/Providers/EventServiceProvider.php
protected $listen = [
\BaksDev\MaterialsCatalog\Events\MaterialCreated::class => [
\App\Listeners\LogMaterialCreation::class,
],
];
// app/Providers/AppServiceProvider.php
public function boot()
{
\BaksDev\MaterialsCatalog\Events::dispatch(
\BaksDev\MaterialsCatalog\Events\MaterialCreated::class,
function ($event) {
event(new \App\Events\MaterialCreated($event->material));
}
);
}
Schema::defaultStringLength() to match the package’s expectations (e.g., 191 for MySQL).Schema::create('materials', function (Blueprint $table) {
$table->id();
$table->string('name', 255);
// ... other fields from the package's migration
});
AppServiceProvider:
public function register()
{
$this->app->singleton(\BaksDev\MaterialsCatalog\Service::class, function ($app) {
return new \BaksDev\MaterialsCatalog\Service(
$app->make(\BaksDev\MaterialsCatalog\MaterialRepository::class)
);
});
}
$this->mock(\BaksDev\MaterialsCatalog\Service::class, function ($mock) {
$mock->shouldReceive('findAll')->andReturn(collect([$material]));
});
resources/lang:
// config/materials-catalog.php
'locale' => app()->getLocale(),
__() function in views:
{{ __('materials-catalog::messages.create_success') }}
$materials = Cache::remember('materials_active', now()->addHours(1), function () {
return Material::where('is_active', true)->get();
});
Schema::table('materials', function (Blueprint $table) {
$table->index('name');
$table->index('category_id');
});
new \BaksDev\MaterialsCatalog\Service()) will fail.$this->app->bind(
\BaksDev\MaterialsCatalog\Service::class,
\BaksDev\MaterialsCatalog\Service::class
);
ContainerNotFoundException in Laravel logs.// app/Facades/Material.php
public static function query()
{
return app(\BaksDev\MaterialsCatalog\MaterialRepository::class)->query();
}
make:material Artisan command).grep -r "public function" vendor/baks-dev/materials-catalog/src/
tests/ for usage examples.How can I help you explore Laravel packages today?