Skizo

Query to Eloquent model

Posted 1 year ago by Skizo

I have this query:

SELECT authors.name, COUNT(books.id) books_count
FROM books
LEFT JOIN authors on authors.id = books.author_id
GROUP BY author.id

How can I transform this into Eloquent models?

I tried this:

class Book extends Model {
    public function author() {
        return $this->belongsTo('Author');
    }
}

Book::all()->with('author');

It worked but the group by is the problem..

Thanks.

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