eloquent count relation

Posted 1 month ago by man_or_astroman

Hello, I have 2 tables, tours and destinations. Tour belongsTo destination and destinations hasMany tours. Im doing some queries on Tour model and I want to show after the query how many tours has each destination. Below is my code:

$destinations = $tours->groupBy('tour_destination_id')->select('tour_destination_id', \DB::raw('count(*) as total'))->with('destination')->get();

With the above code I am able to show the count of each destination that HAS some tours, but I want to show all the destinations even if they don't have any tour(with a total: 0).

Can anyone help me on this? Thank you

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

Reply to

Use Markdown with GitHub-flavored code blocks.