Installation Add the package via Composer:
composer require chill-project/one-stat
Publish the config (if available) and migrations:
php artisan vendor:publish --provider="ChillProject\OneStat\OneStatServiceProvider" --tag="config"
php artisan vendor:publish --provider="ChillProject\OneStat\OneStatServiceProvider" --tag="migrations"
Run migrations:
php artisan migrate
First Use Case
Fetch basic stats for a child (e.g., ID 123):
use ChillProject\OneStat\Facades\OneStat;
$stats = OneStat::getChildStats(123);
dd($stats);
total_visits, last_updated).Fetching Stats
$stats = OneStat::getChildStats($childId);
$regionStats = OneStat::getRegionStats('BRU'); // Brussels
$stats = OneStat::query()
->where('date', '>=', now()->subDays(7))
->get();
Caching Responses Cache frequent requests (e.g., dashboard stats) for 5–15 minutes:
$stats = Cache::remember("one_stat_child_{$childId}", now()->addMinutes(10), function() use ($childId) {
return OneStat::getChildStats($childId);
});
Event Listeners Trigger actions on stat updates (e.g., notify admins if visits drop):
// In EventServiceProvider
protected $listen = [
\ChillProject\OneStat\Events\StatsUpdated::class => [
\App\Listeners\AlertLowVisits::class,
],
];
API Integration Expose stats via Laravel API:
Route::get('/stats/child/{id}', function ($id) {
return response()->json(OneStat::getChildStats($id));
});
childId/region inputs before passing to the package.throttle middleware if the external API has limits.try {
$stats = OneStat::getChildStats($id);
} catch (\Exception $e) {
Log::error("ONEStat fetch failed for child {$id}: " . $e->getMessage());
throw $e;
}
API Rate Limits
sleep() or queue delayed jobs:
sleep(1); // Add delay between requests
OneStat::dispatchSyncStatsUpdate($childIds)->onQueue('stat-updates');
Data Mismatches
childId in your DB matches the external API’s format (e.g., string vs. integer).$stats = OneStat::getChildStats($id) ?? collect(['visits' => 0]);
Time Zones
$stats->last_updated = $stats->last_updated->setTimezone('Europe/Brussels');
Configuration Quirks
config/onestat.php for:
https://api.onestat.be)..env:
ONESTAT_API_KEY=your_key_here
OneStat::setDebug(true); // Logs API requests/responses
Http::fake([
'api.onestat.be/*' => Http::response(['visits' => 5], 200),
]);
Custom Endpoints Extend the package by adding new methods to the facade:
// In OneStatServiceProvider
$this->app->extend('onestat', function ($service) {
$service->addMethod('getCustomStats', function ($id) {
return $this->callExternalApi("custom/{$id}");
});
return $service;
});
Webhooks If the external API supports webhooks, create a Laravel route to handle callbacks:
Route::post('/onestat/webhook', function (Request $request) {
OneStat::handleWebhook($request->all());
});
Database Sync Sync local DB with external stats periodically (e.g., via cron):
* * * * * php artisan onestat:sync
Create a custom Artisan command:
php artisan make:command SyncOneStat
How can I help you explore Laravel packages today?