Installation
composer require ekyna/order-bundle
Add to config/app.php under ExtraBundles:
Ekyna\OrderBundle\EkynaOrderBundle::class,
Database Migration
The bundle requires a database table for orders. Check the migrations/ folder in the bundle for the schema. Run:
php artisan migrate
First Use Case Create an order via the bundle’s service:
use Ekyna\OrderBundle\Service\OrderService;
$orderService = app(OrderService::class);
$order = $orderService->createOrder([
'customer_id' => 1,
'items' => [
['product_id' => 1, 'quantity' => 2],
['product_id' => 3, 'quantity' => 1],
],
]);
Order Creation
Use OrderService to create orders with items, customer references, and metadata:
$order = $orderService->createOrder([
'customer_id' => $customerId,
'items' => [
['product_id' => $productId, 'quantity' => 5, 'price' => 19.99],
],
'status' => 'pending', // Optional; defaults to 'draft'
'metadata' => ['note' => 'Gift order'],
]);
Order Retrieval Fetch orders by ID or filter by status/customer:
// Single order
$order = $orderService->getOrder($orderId);
// Filtered collection
$pendingOrders = $orderService->getOrders(['status' => 'pending']);
Order Updates Modify order status, items, or metadata:
$orderService->updateOrder($orderId, [
'status' => 'shipped',
'metadata' => ['tracking_number' => '12345'],
]);
Order Items Management Add/remove items dynamically:
$orderService->addItemToOrder($orderId, $productId, $quantity);
$orderService->removeItemFromOrder($orderId, $itemId);
order.created, order.status.updated) via Laravel’s event system.
// In EventServiceProvider
protected $listen = [
'Ekyna\OrderBundle\Event\OrderCreated' => [
'App\Listeners\NotifyCustomerOrderCreated',
],
];
OrderValidator class or using Laravel’s built-in validation.Route::apiResource('orders', OrderController::class);
Missing Documentation
README lacks installation/configuration details. Check the src/ directory for undocumented classes (e.g., OrderService, OrderRepository).tests/ folder for usage examples or reverse-engineer from the bundle’s codebase.Database Schema Assumptions
orders, order_items, and customers. Ensure these exist or alias them in the bundle’s configuration.OrderRepository to point to custom tables:
$bundle->setRepository(new CustomOrderRepository());
Status Transitions
draft → pending → shipped). Validate transitions manually or extend the OrderService:
if (!$orderService->isValidStatusTransition($order, 'pending', 'shipped')) {
throw new \RuntimeException('Invalid transition');
}
Dependency Injection
$this->app->bind(OrderService::class, function ($app) {
return new CustomOrderService($app->make(OrderRepository::class));
});
public function handle(OrderCreated $event) {
\Log::info('Order created', ['order_id' => $event->order->id]);
}
try {
$order = $orderService->createOrder($data);
} catch (\Exception $e) {
\Log::error('Order creation failed', ['error' => $e->getMessage()]);
}
Custom Fields
Add fields to the orders table and extend the Order entity:
// In a service provider
$order = new Order();
$order->setCustomField('priority', 'high');
Update the OrderRepository to handle these fields.
Payment Integration
Hook into the order.status.updated event to trigger payment processing:
public function handle(OrderStatusUpdated $event) {
if ($event->newStatus === 'paid') {
$this->processPayment($event->order);
}
}
Testing
Use the bundle’s test suite as a reference. Mock the OrderRepository in unit tests:
$repository = Mockery::mock(OrderRepository::class);
$service = new OrderService($repository);
How can I help you explore Laravel packages today?