Postingan lainnya
[laravel] mencari average pada relationship one to many
Ask Saya punya 2 tabel : 1. player (id, nama) 2. scores (id, player_id, nilai)
Player.php (Model)
public function scores()
{
return $this->hasMany('App\Scores');
}
Scores.php (Model)
public function player()
{
return $this->belongsTo('App\Player');
}
dan querynya :
$players = Player::with('scores')->limit(20)->get();
bagaimana caranya mendapatkan rata-rata dari tabel `scores`.`nilai` ? Terima kasih
0
1 Jawaban:
Pakai fungsi avg(), ada di <a href='https://laravel.com/docs/7.x/collections#method-avg'>Dokumentasi</a>
0
Tanggapan
Terimakasih