Laravel - count records from many tables

1.6k views Asked by At

Is it possible in Laravel create one query which separate count all records from 3 different not related tables? And without using Eloquent.

Example: I have three tables, A, B, C. I can make three separate query's that will count all records in every table. Like "DB::table('A')->count();"

But I need a one query to do this. $all = DB:: ??

And as a result of this variable I will get something like:

$all->A 
$all->B
$all->C

Thank you.

1

There are 1 answers

0
apokryfos On BEST ANSWER

You can just write the raw query:

$result = DB::select("SELECT (SELECT COUNT(*) FROM A) as a, (SELECT COUNT(*) FROM B) as b, (SELECT COUNT(*) FROM C) as c");

Then the counts would be at $result->a, $result->b, $result->c