Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Ok I couldnt think of a way to do this using true models but we can achieve it easy enough using a DB::Raw subquery.

Have a look at this code below. I have models Version and SubVersion which match tables called versions and subversions. You should be able to apply this to your problem.

		
$summary = DB::Raw("(SELECT version_id, COUNT(subversion_id) AS num, SUM(IF(status='Live', 1, 0)) AS numLive FROM subversions GROUP BY version_id) AS subtable ");

$versions = Version::join( $summary , "versions.version_id", "=", "subtable.version_id")->get();
foreach ($versions as $version) {
	echo $version->version_id . " - " . $version->numLive . "<br>";
}

Good luck

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

guidsen guidsen Joined 24 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.