按数据计算laravel

Here I want to add the data I have based id_data kos, so whenever I add data will auto count by id_data_kos.

here my controller :

$limit = 10;
        $result = DB::table('residents')
        ->join("data_kos","data_kos.id","=","residents.id_data_kos")
        ->select("residents.*","data_kos.title as data_title")
        ->groupBy('id_data_kos')
        ->orderBy('residents.id', 'asc');

$data['posts'] = $result->paginate($limit);

and this my view :

@foreach($posts as $row)
<td>{{ $row->data_title }}</td>
<td>here i want to count</td>
@endforeach

this my table :

this

Try this

$limit = 10;
        $result = DB::table('residents')
        ->join("data_kos","data_kos.id","=","residents.id_data_kos")
        ->select("residents.*","data_kos.title as data_title", DB::raw('COUNT(id_data_kos) as count'))
        ->groupBy('id_data_kos')
        ->orderBy('residents.id', 'asc');

$data['posts'] = $result->paginate($limit);

The count of id_data_kos will be returned as count