vendredi 24 juin 2016

How can i include null values for joined tables with where

How can i modify my query to include the vocation code, even if there was no associated results for that code? code should be displayed ( i have 6 vocation codes) and count should be displayed as 0 if there were no entries for that code. $maori = $request->exists('maori'); $years = collect([2011,2012,2013,2014,2015,2016]); $providers = $years->map(function($year) use ($maori){ $query = DB::table('vocations') ->selectRaw('vocations.*,count(*) as count') ->leftjoin('standard_vocation', 'vocations.id','=','standard_vocation.vocation_id') ->leftjoin('standards','standard_vocation.standard_id','=','standards.id') ->leftjoin('results','standards.id','=','results.id') ->leftjoin('learner_provider','results.learner_provider_id','=','learner_provider.id'); if($maori) { $query->join('learners','learner_provider.learner_id','=','learners.id') ->join('learner_ethnicity','learners.id','=','learner_ethnicity.learner_id') ->leftjoin('ethnicities','ethnicities.id','=','learner_ethnicity.ethnicity_id') ->where('ethnicities.code','211'); } $query->where('learner_provider.year',$year); $query->groupBy('vocations.id'); return $query->get(); });

Aucun commentaire:

Enregistrer un commentaire