🧠 The Problem
Imagine you’re building a dashboard for a matrimonial website (😉), and you want to show:
- Daily user registrations over the past month
- Monthly profile views over the past year
- Yearly revenue trends from premium subscriptions
You could write raw SQL queries or complex Eloquent logic to group data by date intervals, but that quickly becomes messy:
- You need to manually format timestamps
- Handle gaps in data (e.g., days with zero entries)
- Ensure performance with large datasets
- Maintain readability and reusability
This is where most Laravel devs hit a wall: “How do I aggregate time-series data cleanly and efficiently?”
✅ The Solution: Laravel Trend
Laravel Trend abstracts all that boilerplate into a fluent, expressive API. Let’s solve each problem step-by-step.
📅 Problem 1: Daily User Registrations
You want to show a chart of how many users registered each day for the past 30 days.
✅ Solution
use Flowframe\Trend\Trend;
$dailyRegistrations = Trend::model(User::class)
->between(
start: now()->subDays(30),
end: now()
)
->perDay()
->count();
This returns an array like:
[
['date' => '2025-08-10', 'aggregate' => 12],
['date' => '2025-08-11', 'aggregate' => 9],
...
]
Perfect for feeding into Chart.js or ApexCharts.
📆 Problem 2: Monthly Profile Views
You track profile views in a profile_views table. You want to show monthly trends for the past year.
✅ Solution
$monthlyViews = Trend::model(ProfileView::class)
->between(
start: now()->subYear(),
end: now()
)
->perMonth()
->count();
No need to manually group by MONTH(created_at) or worry about missing months—it handles that for you.
💰 Problem 3: Yearly Revenue from Subscriptions
You want to sum up the amount column from subscriptions table, grouped by year.
✅ Solution
$yearlyRevenue = Trend::model(Subscription::class)
->between(
start: now()->subYears(5),
end: now()
)
->perYear()
->sum('amount');
You can also use average(), max(), or min() depending on your metric.
🧪 Bonus: Filtering with Conditions
Let’s say you only want to count users from India:
$indianUsers = Trend::query(
User::where('country', 'India')
)
->between(
start: now()->subMonth(),
end: now()
)
->perDay()
->count();
Using query() gives you full control over filtering.
🧠 Final Thoughts
Laravel Trend isn’t just a convenience—it’s a problem-solving tool for any Laravel developer dealing with time-based data. It helps you:
- Avoid repetitive SQL logic
- Write clean, readable code
- Handle edge cases like empty intervals
- Focus on insights, not implementation
If you’re building dashboards, analytics APIs, or admin panels, this package can save hours of work and make your codebase more maintainable.
Fuel my creative spark with a virtual coffee! Your support keeps the ideas percolating—grab me a cup at Buy Me a Coffee and let’s keep the magic brewing!
