The query won't be the same, but it'll get you what you want using Eloquent. This assumes you have the relationship books() defined on the Author model.
Well right now you use the relationships function from Laravel. It's actually doing two queries here.
// returns multiple items with an author_id column
select * from books
// The author ids will be used in the below query
select * from author where id IN (1, 2, 3); //
After that Laravel will then connect the related items and return them in one collection.
If you want to group by a relation you need to use a join query, just like your raw query
Book::select(DB::raw('authors.name', 'count(books.id) as books_count'))
->join('authors', 'authors.id', '=', 'books.author_id')
->groupBy('authors.id')
->get();
@devk The query above is simplified version of the actuall query so I dont really can use that.
@bobbybouwmann I used this method already, I thought there are ways to use Eloquent relationships.