lundi 25 juin 2018

How to have the sum of a related models column in Eloquent

I have a hasMany relation on my user model

public function timesheet()
    {
        return $this->hasMany('App\TimeTable', 'employee');
    }

and I would like to have the sum of a column

$users = User::with('timesheet')->get();

foreach($users as $user) {
    //sume of $user->timesheet
    //tried $user->timesheet->sum('minutes') which return on each iteration 0
}

What is the best approach to get the right result?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire