To achieve selling items as a set (bundle) in your Laravel application, you'll need to extend your database structure. The core idea here is:
- Define "Set" Products: A set (like "AYZ") is itself a product, with its own name and price.
- Link Items and Quantities to Sets: Each set contains several items, each with specific quantities.
- Handling Sale: When a customer buys the set, you store that, but internally, you also know which items and how many of each are being sold.
1. Database Structure Additions
a) Create product_sets Table
This table will represent a "set" of items.
php artisan make:model ProductSet -m
Migration example:
Schema::create('product_sets', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->decimal('price', 10, 2); // price for each set
$table->timestamps();
});
b) Create Pivot Table: product_set_items
This connects a set to items and specifies quantities.
Schema::create('product_set_items', function (Blueprint $table) {
$table->id();
$table->foreignId('product_set_id')->constrained()->onDelete('cascade');
$table->foreignId('inventory_item_id')->constrained()->onDelete('cascade');
$table->integer('quantity'); // pieces per set
$table->timestamps();
});
2. Model Relationships
ProductSet.php
class ProductSet extends Model
{
protected $fillable = ['name', 'price'];
public function items()
{
return $this->belongsToMany(InventoryItem::class, 'product_set_items')
->withPivot('quantity');
}
}
InventoryItem.php
Add this:
public function productSets()
{
return $this->belongsToMany(ProductSet::class, 'product_set_items')
->withPivot('quantity');
}
3. Creating and Selling a Set
- When creating a set, the admin chooses which items it contains and their default quantities.
- When a customer buys a set, you:
- Add a record to
sold_inventory_itemsfor the set as one line item (with its specific set price). - Optionally, for stock management, deduct the correct multiplied quantities of each component item from your inventory.
- Add a record to
Example: Calculating Item Quantities for Multiple Sets
Suppose a customer orders 2 AYZ sets, and AYZ is defined as:
- A: 2pcs
- Y: 3pcs
- Z: 5pcs
Code sample to get the per-item quantities for any number of sets:
$set = ProductSet::with('items')->where('name', 'AYZ')->first();
$numberOfSets = 2;
// Build a list of items and total quantities needed
foreach ($set->items as $item) {
$itemName = $item->item->name; // assuming you have $item->item relationship
$totalQty = $item->pivot->quantity * $numberOfSets;
echo "{$itemName} = {$item->pivot->quantity}pcs * {$numberOfSets} = {$totalQty}pcs\n";
}
4. Selling a Set (Livewire/Controller Example)
When a buyer orders AYZ 2 sets:
- Add a line to
sold_inventory_itemswithinventory_in_store_idset to the set (or a dummy/set record),quantityas 2,unit_priceas $100,sub_totalas $200. - For inventory management, decrement each component item in your store's inventory:
foreach ($set->items as $item) {
$totalQty = $item->pivot->quantity * $numberOfSets;
// Find inventory in store and reduce quantity
$invInStore = InventoryInStore::where('inventory_item_id', $item->id)
->where('store_id', $storeId)
->first();
if ($invInStore) {
$invInStore->quantity -= $totalQty;
$invInStore->save();
}
}
5. Optional: Reporting/Breakdown
Store only the set in sold_inventory_items, but when displaying an invoice breakdown, retrieve the set components and multiply by sets sold to show actual item distribution.
Summary
- Add
product_setsandproduct_set_items(pivot) tables. - Relate sets to items, with quantities.
- When selling a set, sell by set price and adjust inventory by breaking down to actual component usage.
- All logic can be wrapped into service classes or inside your Livewire actions.
If you want concrete migration or Livewire sample code, let me know!