php - Getting value from query builder select count result in Laravel

280

I have a query builder in Laravel controller to get total value of total select table row from database, and I want to get the result value, but the result always give me an array result, and I've tried to access the value from the array but I still cant get the value

Here's the query builder:

$total = DB::select(DB::raw("SELECT COUNT(*) FROM hstmemb WHERE period = '".$bonperiod."' AND totbns <> 0 AND paytype = 'S'"));
$getbonus[0]->total = $total;

The result I get is an array object, but I want to get just the count result in int number.

617

Answer

Solution:

Use DB table with count function.

$total = DB::table('hstmemb')->where('period', $bonperiod)->where('totbns', '<>', 0)->where('paytype', 'S')->count();

People are also looking for solutions to the problem: Php function as function's parameter

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.