hunterhawley
4 months ago

Call model function in "the plural" sense?

Posted 4 months ago by hunterhawley

Hey y'all!

Quick background:

I have three models that are working together here: Seasons, Games, and Stats.

Seasons will have multiple games, Games will have multiple stats.

On the Stats model, I've created a function that I'm trying to make do a lot. I want this function to take 6 arguments.

  1. The type of stat. Easy to get, just have to use a where()
  2. The aggregation level. Basically, do I want to get all the stats inside a game, or inside a season. This one took me a while to make work, but I got it done.
  3. The exact aggregation point. Say I want to aggregate by game in the last arg. In this one, I'll put the ID of the game that I want to see the stats for.

4, 5, and 6 are irrelevant to this exact issue.

So, I can make all of this work, but there is a catch. I have to do this:

@foreach($stats as $stat)

 {{$stat->statPlacement(2, 'game', 1, 'team', 9, $formatted = false)}}

@endforeach

I'd like not to have to use a foreach. Essentially, this function goes and counts every time this particular type of stat happens inside the parameters of the given aggregation. That being said, if I use a foreach, I am just returning the same number a whole lot of times. I just want it once.

I'm probably approaching this problem wrong, and I may not even need to do this in the model, but let me know what you think.

Thanks y'all!

Hunter

Please sign in or create an account to participate in this conversation.