SELECT books.*, count(orders.book_id) as number_of_orders
from books
left join orders
on (books.book_id = orders.book_id)
group by
books.book_id
Book::withCount('orders')->get();
DB::table('books')
->join('orders', function ($join) {
$join->on('books.book_id', '=', 'orders.book_id');
})
->groupBy('books.book_id');
->select('books.*', DB::raw('count(orders.book_id) as number_of_orders'))
->get();