I have surveys and survey_votes table. the table structure is like
Table: surveys
- id
- title
- ... other fields
Table: survey_votes
- id
- ... other fields
- survey_id
- mark ( Number of votes )
and the relationship between these 2 tables is like.
class Survey extends Model
{
public function votes() {
return $this->hasMany(SurveyVote::class);
}
}
class SurveyVote extends Model
{
public function survey() {
return $this->belongsTo(Survey::class);
}
}
Now I fetching surveys with survey votes using with(). but getting empty results.
$surveys = Survey::with(['votes' => function ( $query ) {
$query->select(\DB::raw('SUM(mark) total_votes'))->groupBy('survey_id');
}])->get();
and the strange thing is when I remove this $query->select()
then it's working well. I have referred to all the links regarding this issue, but not work.
Note: I am using Laravel v5.8
Your suggestion is valuable for me, Thanks in Advance
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire