Installation Add the package via Composer:
composer require atm/ordertrackerbundle
Register the bundle in config/app.php under providers:
ATM\OrderTrackerBundle\OrderTrackerServiceProvider::class,
Publish migrations and config:
php artisan vendor:publish --provider="ATM\OrderTrackerBundle\OrderTrackerServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="ATM\OrderTrackerBundle\OrderTrackerServiceProvider" --tag="config"
Run migrations:
php artisan migrate
First Use Case: Tracking an Order
Inject the OrderTracker service into a controller or service:
use ATM\OrderTrackerBundle\Services\OrderTracker;
public function __construct(OrderTracker $orderTracker) {
$this->orderTracker = $orderTracker;
}
Create a new order tracking record:
$trackingData = [
'order_id' => 'ORD12345',
'status' => 'processing',
'customer_id' => 1,
'notes' => 'Initial order received',
];
$this->orderTracker->createTrackingRecord($trackingData);
Key Configurations
Check config/ordertracker.php for default settings (e.g., statuses, event listeners). Override as needed:
'statuses' => [
'processing' => 'Processing',
'shipped' => 'Shipped',
'delivered' => 'Delivered',
'cancelled' => 'Cancelled',
],
Order Lifecycle Management
Use the OrderTracker service to update statuses and log events:
// Update status and log a note
$this->orderTracker->updateStatus('ORD12345', 'shipped', 'Shipped via FedEx');
// Add a custom event (e.g., for analytics)
$this->orderTracker->logEvent('ORD12345', 'tracking_updated', ['carrier' => 'FedEx']);
Integration with Eloquent Models
Attach the bundle to an existing Order model:
use ATM\OrderTrackerBundle\Traits\Trackable;
class Order extends Model {
use Trackable;
protected $trackableType = 'order';
}
Now, use trackable() to interact with tracking records:
$order = Order::find(1);
$order->trackable()->updateStatus('shipped');
Event-Driven Updates
Listen for order events (e.g., order.placed) and trigger tracking updates:
// In EventServiceProvider
protected $listen = [
'order.placed' => [
'ATM\OrderTrackerBundle\Listeners\CreateTrackingRecord',
],
];
API Endpoints Expose tracking data via API:
Route::get('/orders/{order}/track', function ($order) {
return $order->trackable()->withStatusHistory()->first();
});
statuses config array or create a dynamic loader.OrderTracker to trigger webhooks on status changes:
$this->orderTracker->onStatusChange('ORD12345', function ($oldStatus, $newStatus) {
// Send webhook
});
$orderIds = ['ORD123', 'ORD456'];
$this->orderTracker->bulkUpdateStatus($orderIds, 'shipped');
Migration Conflicts
trackables table, ensure the bundle’s migrations don’t overwrite your schema. Use --force cautiously:
php artisan migrate --force
Status Validation
statuses config array. If you add a new status dynamically, ensure it’s included in the config or handled via a custom validator:
$this->orderTracker->setAllowedStatuses(['processing', 'shipped', 'custom_status']);
Performance with Large Datasets
trackable() relationships on every request. Use with() selectively:
// Bad: Loads all tracking records for every order
$orders = Order::with('trackable')->get();
// Good: Load only what you need
$order = Order::with(['trackable' => function ($query) {
$query->where('status', 'shipped');
}])->find(1);
Event Listener Order
order.status_updated), ensure the OrderTracker listener runs last to avoid race conditions.config/ordertracker.php:
'debug' => env('APP_DEBUG', false),
OrderTracker calls in try-catch to log errors:
try {
$this->orderTracker->updateStatus($orderId, 'shipped');
} catch (\Exception $e) {
\Log::error("Tracking update failed: " . $e->getMessage());
}
Custom Trackable Models
Extend the Trackable trait to add model-specific logic:
use ATM\OrderTrackerBundle\Traits\Trackable;
class Order extends Model {
use Trackable;
protected function getTrackableType(): string {
return 'custom_order_type';
}
}
Override Status Transitions
Use the status_transitions config to enforce rules (e.g., prevent going from shipped to processing):
'status_transitions' => [
'processing' => ['shipped', 'cancelled'],
'shipped' => ['delivered', 'cancelled'],
],
Add Custom Fields
Extend the trackables table via a migration and update the Trackable model:
// Migration
Schema::table('trackables', function (Blueprint $table) {
$table->string('custom_field')->nullable();
});
// Model
protected $fillable = ['custom_field'];
Localization Override status labels in language files:
// resources/lang/en/ordertracker.php
return [
'statuses' => [
'processing' => 'Under Review',
'shipped' => 'On Its Way',
],
];
How can I help you explore Laravel packages today?