baks-dev/reference-materials-furniture
Installation:
composer require baks-dev/reference-materials-furniture
Ensure your composer.json includes "require": {"php": "^8.4"}.
Publish Config (if needed):
php artisan vendor:publish --provider="BaksDev\Furniture\FurnitureServiceProvider" --tag="config"
Check config/furniture.php for default material definitions (e.g., wood_types, upholstery_materials).
First Use Case: Fetch a material by ID in a Blade template or controller:
use BaksDev\Furniture\Facades\Furniture;
$oakMaterial = Furniture::getMaterial('oak');
dd($oakMaterial->name, $oakMaterial->density); // Output: "Oak", 0.72
Key Files to Review:
config/furniture.php: Default material catalog and settings.src/Facades/Furniture.php: Main facade for quick access.src/Contracts/MaterialInterface.php: Expected material structure.Material Retrieval:
Furniture::getMaterial($id) returns a Material object.Furniture::allMaterials() returns a Collection of all materials.Furniture::filterMaterials(fn($material) => $material->type === 'wood').Dynamic Material Extension:
'custom_materials' => [
'bamboo' => [
'name' => 'Bamboo',
'type' => 'wood',
'density' => 0.65,
'sustainable' => true,
],
],
Furniture::addMaterial(new \BaksDev\Furniture\Models\Material([
'id' => 'reclaimed_oak',
'name' => 'Reclaimed Oak',
'type' => 'wood',
'density' => 0.8,
]));
Integration with Eloquent:
morphTo:
class Chair extends Model {
public function material() {
return $this->morphTo();
}
}
$valid = Furniture::isValidMaterial($request->material_id);
Localization:
lang/en/furniture.php:
return [
'materials' => [
'oak' => [
'name' => 'American White Oak',
'description' => 'Durable and water-resistant...',
],
],
];
API Responses:
Furniture::materialsAsJson():
return response()->json(Furniture::materialsAsJson(['wood']));
Material ID Case Sensitivity:
config/furniture.php must match exactly (e.g., 'oak' vs 'Oak'). Use strtolower() when accepting user input:
$normalizedId = strtolower($request->material_id);
Config Caching:
php artisan config:clear
refresh() method:
Furniture::refresh();
Missing Default Config:
config/furniture.php exists, the package falls back to hardcoded defaults. Publish the config to customize:
php artisan vendor:publish --tag="config" --provider="BaksDev\Furniture\FurnitureServiceProvider"
Type Safety:
MaterialInterface enforces id, name, type, and density fields. Omitting these will throw InvalidArgumentException.Performance:
Furniture::allMaterials() in loops. Cache the result:
$materials = Furniture::remember(3600, fn() => Furniture::allMaterials());
Validate Material Data:
validateMaterial() helper:
if (!Furniture::validateMaterial(['id' => 'oak', 'name' => 'Oak'])) {
throw new \InvalidArgumentException("Invalid material data.");
}
Check for Overrides:
config/furniture.php override defaults. Use Furniture::getRawMaterials() to inspect the merged catalog.Facade Binding:
$this->app->bind('Furniture', function ($app) {
return new \BaksDev\Furniture\Facades\Furniture($app['BaksDev\Furniture\MaterialRepository']);
});
Custom Material Providers:
BaksDev\Furniture\Contracts\MaterialProvider to fetch materials from an external API or database:
class ApiMaterialProvider implements MaterialProvider {
public function getMaterials(): array {
return json_decode(file_get_contents('https://api.example.com/materials'), true);
}
}
config/furniture.php:
'providers' => [
\App\Providers\ApiMaterialProvider::class,
],
Material Events:
MaterialAdded):
event(new \BaksDev\Furniture\Events\MaterialAdded($material));
EventServiceProvider:
protected $listen = [
\BaksDev\Furniture\Events\MaterialAdded::class => [
\App\Listeners\LogMaterialAddition::class,
],
];
Testing:
$this->mock(Furniture::class)->shouldReceive('getMaterial')
->with('oak')->andReturn(new Material(['id' => 'oak', 'name' => 'Mock Oak']));
FurnitureTestCase trait for shared assertions:
use BaksDev\Furniture\Testing\FurnitureTestCase;
class MaterialTest extends FurnitureTestCase {
public function testOakMaterialExists() {
$this->assertMaterialExists('oak');
}
}
Blade Directives:
Blade::directive('material', function ($expression) {
return "<?php echo \\BaksDev\\Furniture\\Facades\\Furniture::getMaterial({$expression})->name; ?>";
});
@material('oak') is a popular choice for furniture.
How can I help you explore Laravel packages today?