samedi 27 janvier 2018

Calling Laravel relationship with aggregate

If I have a Laravel 5.5 model called User that hasMany Posts and each Post hasMany Hits, is there an aggregate function I can call to get the total number of Hits for a User across all Posts, where the Hit was created in the last week?

It seems like there may be a clever way to do it besides doing something like

$hits = $user->posts()->hits()

and then looping over those hits to check created date.

In this case it seems like raw sql would be better, but I figured there may be an Eloquent way to handle a situation like this.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire