lundi 16 septembre 2019

How to get count and avg of results in Laravel 5.8

I have the below query returning all available fiction books from all libraries:

$results = Library::with('category.books')
    ->whereHas('category.books', function($query) {
        $query->where('available', 1);
    })
    ->whereHas('category', function($query) {
        $query->where('name', 'fiction');
    })
    ->get();

Now, what is the best way to get the total number of books and the average rating per book (book has field rating), per library?

I assume I have to create a collection of these results somehow, apply a custom function.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire