laravel 4 - Query builder with max() -


i trying rewrite sql laravel 4

select     *     solutionfile     solutionfile.group_id = $group_id ,     version = (          select              max(s1.version)                       solutionfile s1                       s1.group_id = $group_id     ) 

i wrote request

solutionfile::where('group_id', '=', $group_id)     ->whereraw('version = (select max(`version`) files group_id = ' . $group_id . ')')->get(); 

which works perfectly, want rewrite in "laravel way" without whereraw. tried request

solutionfile::where('group_id', '=', $group_id)     ->where(function($query) use($group_id) {             return $query->where('version', '=', function() use($group_id) {                 return solutionfile::where('group_id', '=', $group_id)->max('version');             });         }); 

but returns empty set.

is there way rewrite it? did make mistake in last request?

i found simple solution this

solutionfile::where('group_id', '=', $group_id)                         ->where(function($query) use($group_id) {                             return $query->where('version', '=', solutionfile::where('group_id', '=', $group_id)->max('version'));                         })->get() 

there no need use function() third param, result function.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -