5 months ago

Custom collections in child models

Posted 5 months ago by placeholderMeme

what is best practice when you need to calculate custom collections in child model?

I have 2 Models- Car and Refill, Car has many Refills. I want to calculate average fuel consumption by month which basically creates new collection from refills entries, currently I have static class within Refill model that accepts car id as parameter, then does select query and iterates through results.

Is it possible to do something like $car->refills()->getAverageMpgByMonth() instead of Refills::getAverageMpgByMonth($carId).

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