Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jduke's avatar
Level 1

Help streamlining data collections

Hello to anyone who would be so kind to share some insight on Laravel design architecture. The preamble is kind of long so let me know if I am able to reward/reciprocate in someway.

I'm new to Laravel and designing an admin dashboard that tracks a company's recruiter/hiring processes. I have tables for Job, Opening, Application, Candidate. The last 3 are related to job either using hasMany or hasManyThrough.

What I am facing is my JobController starting to bloat. Some calculations are done in the frontend (Vue) but I'm trying to return mostly polished data. I am using Inertia so data manipulation in my templates is limited.

Data being sent to the dashboard view/page

  • list of all open jobs
  • totals for jobs, applications, candidates (grouped by the 'priority' column in the jobs table)
  • jobs grouped by
    • department
    • application stage
    • jobs that have had no new activity in [week, month, quarter] timeframes
    • source
  • average of all jobs average open time
  • stats for the last [week, month] which includes:
    • jobs with new [openings, applications, submissions]

My current approach

I am querying for all jobs once (eager loading applications and openings) and then for each output mentioned above, I've run that collection through eloquent methods until I'm returning the data structure desired. Some of the eloquent chaining is feeling a little repetitive -- especially for any timeframe output.

Here's a look at stale jobs (in this particular code, it's referencing an application model attribute called 'fresh' that is an array of [week, month, quarter] and returns a boolean.

'stale' => $collection->filter( function($job) {
  return collect($job->applications)
      ->filter( function($application) {
          return !$application->fresh['week'];
      })
      ->isNotEmpty();
  })
  ->map( function($job) {
      $latest = collect($job->applications)
          ->mapWithKeys(function($application) {
              $humanTime = (int) Carbon::parse($application->last_activity_at)->diffInDays();
              return [$application['id'] => $humanTime];
          })
          ->sort()
          ->first();

      return [
          'id' => $job->id,
          'name' => $job->name,
          'department' => $job->department,
          'priority' => $job->priority,
          'days_since_last_activity' => (int) $latest
      ];
  })
  ->sortByDesc( function($job) {
      return $job['days_since_last_activity'];
  })
  ->values()
  ->take(10)
  ->toArray(),

You can see how having all these chained eloquent collections for each of the desired data output can lead to a lengthy controller.

So:

  • how bad is my use of eloquent?
  • should I try to move this logic anywhere?
  • does anyone tend to store the output of the less time-sensitive data in its own table?

Thanks to all in advance.

0 likes
0 replies

Please or to participate in this conversation.