Displaying relation count in a column

Hi,

I have an event table, a users table and a role table, they're linked through a pivot table named event_user_roles. This represents, what role a user is assigned for a given event. I have relations set up properly in the models. I would now like to display the number of users with a certain role in a upcomingEvents widget. How do I do that?

The corresponding eloquent query would be:
$event->users()->join('roles', 'event_user_roles.role_id', '=', 'roles.id')
  ->select('events.id', \Illuminate\Support\Facades\DB::raw('SUM(if((roles.role = "kitchen"), 1, 0)) AS kitchen_count'))
  ->get();


How does that translate into my widget column?
Was this page helpful?