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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community