- How do I install Laravel Metrics for Laravel 11/12?
- Run `composer require eliseekn/laravel-metrics` in your project directory. The package requires PHP 8.2+ and Laravel 11/12. For Laravel 13, check the 4.x branch in the GitHub repo.
- Can I use this package with non-Eloquent queries (e.g., raw DB tables)?
- Yes. Use `DB::table('your_table')` or any Query Builder instance with `LaravelMetrics::query()`. It supports custom tables, joins, and even non-model data.
- Does Laravel Metrics support PostgreSQL or SQLite?
- Yes, the package is tested and supported on MySQL, PostgreSQL, and SQLite. The changelog notes fixes for PostgreSQL/SQLite edge cases, but ensure your queries are indexed for performance.
- How do I generate monthly trends for a model like `Order`?
- Use the fluent chain: `LaravelMetrics::query(Order::query())->sum('amount')->byMonth(6)->trends()`. This returns an array of monthly sums for the last 6 months. Replace `sum()` with `count()` or other aggregations.
- Can I customize the date column (e.g., use `published_at` instead of `created_at`)?
- Yes. Add `.dateColumn('published_at')` to your chain. This overrides the default `created_at` column for time-based grouping (e.g., `byMonth()`, `byDay()`).
- Will this work for real-time dashboards (e.g., live updates)?
- No. Laravel Metrics is optimized for batch/periodic metrics (e.g., daily/monthly trends). For real-time data, consider caching results (e.g., Redis) or a dedicated service like Laravel Echo with WebSockets.
- How do I group metrics by a custom column (e.g., `status` or `category`)?
- Use `.labelColumn('status')` or `.labelColumn('category')` in your chain. This groups the output by the specified column, useful for segmented analytics like `count by status`.
- Does Laravel Metrics integrate with frontend frameworks like Chart.js or Inertia?
- No. The package returns raw arrays/collections (e.g., `[['month' => 'Jan', 'value' => 100]]`). You’ll need to process this data in your frontend (e.g., Chart.js) or Laravel views.
- What if my database queries are slow for large datasets?
- Add indexes to date/aggregation columns (e.g., `created_at`, `amount`). For frequent requests, cache results in Redis or implement a scheduled job to precompute metrics. Avoid large date ranges (e.g., `between('2010-01-01', now())`) without optimization.
- Are there alternatives for Laravel metrics with caching or real-time support?
- For caching, pair this with Laravel’s cache or Redis. For real-time, consider `spatie/laravel-analytics` (event-based) or `laravel-echo` with a queue. If you need a single package, `beberlei/doctrineextensions` (for Doctrine) or `laravel-trends` (legacy) are alternatives, but Laravel Metrics offers deeper Eloquent integration.