jeudi 5 mai 2016

not working laravel php dynamic query?

can anyone help me to change this as dynamic query. I have tried many times but i am not getting the result.

$year = Input::get('Year');
$summary = DB::select("SELECT Tower, SUM( CASE  WHEN project_year=$year THEN Net_Productivity ELSE '0' END )/'1000000' AS 'ttt', SUM( CASE  WHEN Region = 'Africa' and project_year=$year THEN Net_Productivity ELSE '0' END )/'1000000' AS 'test1', SUM( CASE WHEN Region = 'Europe' and project_year=$year THEN Net_Productivity ELSE '0' END )/'1000000' AS 'test2' FROM trackers WHERE Status != 'Dropped' GROUP BY Tower");

This is working fine. But i want to pass the region name to dynamically.

If i have the region names in the below variable like

$regions = DB::table('trackers')->select('Region')->distinct()->get();

This is my try. But i dont know where i am wrong.

$summary = DB::table('trackers')
            ->select('Tower', function($region) {
                foreach ($regions as $value) {
                    //                        
                    $region->select(DB::raw('CASE WHEN Region = $value->Region and project_year=$year THEN Net_Productivity ELSE 0 END as $value->Region'));                      
                })
            }
            ->groupBy('Tower')
            ->get();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire