withCount
withCount([
'relational-table' => [
'foreign-id-in-the-selected-table',
'primary-key-column-of-the-relational-table'
]
])
// using the withCount
Route::get('/withCount', function () {
$users = DB()->selectLoop("*","users")
->withCount([
"projects" => ['id', 'user_id']
])->get();
foreach($users as $user){
echo "Total Project of ".$user['fullname'].": ". $user['projects_count'];
};
});
Last updated