Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tomasosho's avatar

get value from query as integer

How do i get the present_count value from the query below as an integer?

$attendance = Attendance::selectRaw('count(present) as present_count')
            ->whereMonth('date', $month)
            ->whereYear('date', $year)
            ->where('staff_id', $request->input('user_id'))
            ->get()->toArray();
0 likes
4 replies
jlrdw's avatar

Cast to int if needed. But in php should just work as integer anyway unlike some languages.

Are you getting expected results.

2 likes
tomasosho's avatar

Yes i am getting my expected value, but i'd love to get my present_count as just integer outside the query. right now my result is

array:1 [▼
  0 => array:1 [▼
    "present_count" => 2
  ]
]

i want it to be like this instead

2
tykus's avatar
tykus
Best Answer
Level 104

If you want $attendance as the integer result only:

$attendance = Attendance::whereMonth('date', (int) $month)
    ->whereYear('date', (int) $year)
    ->where('staff_id', $request->input('user_id'))
    ->count('present');
3 likes
tomasosho's avatar

I've been getting 0 with this

[$year, $month]= explode('-', $day->date);// e.g. [2021, 7]
        $check = IOU::whereMonth('date', '=',(int)$month)
            ->whereYear('date', (int)$year)
            ->where('staff_id', $request->input('staff_id'))
            ->sum('amount');

When i change it to

[$year, $month]= explode('-', $day->date);// e.g. [2021, 7]
        $check = IOU::whereMonth('date', '=',(int)$month)
            ->whereYear('date', (int)$year)
            ->where('staff_id', $request->input('staff_id'))
            ->get('amount');

and i dd it i get []. and it's not throwing in any error

Please or to participate in this conversation.