php - how we can count items by id in database using laravel

808

count items by id in sub category table

$count = DB::table('sub_category')
               ->select count(*) as 
               ->where('cat_id', $id)

error show

FatalErrorException in BasicController.php line 51:

syntax error, unexpected 'count' (T_STRING)

557

Answer

Solution:

You should try this. Laravel Query builders provide Aggregates

$count = DB::table('sub_category')->where('cat_id', $id)->count();

If you still want to go for raw queries,

DB::table('sub_category')
                 ->select(DB::raw('count(*) as category_count'))
                 ->where('cat_id', $id)
                 ->get();

People are also looking for solutions to the problem: php - API send email and Password using CURL

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.