Carbon has a method to calculate the "age" of a date, so you can do this in your model instead:class Member extends Model{ protected $casts = [ 'birthDate' => 'date', ]; public function getAgeAttribute() { return $this->birthDate->age; }}如果您要计算多个成员的平均年龄,那么我将创建一个类,该类接受一组成员并返回该值:If you’re wanting to calculate the average age of multiple members, then I’d create a class that takes a collection of members and returns that value:class AverageAge{ public static function forMembers(Collection $members) { return $members->average(function ($member) { return $member->age; }); }}然后您可以像这样使用:Which you can then use like this:$members = Member::where(...)->get();$averageAge = AverageAge::forMembers($members); 这篇关于如何使用使用者的出生日期来计算使用者的平均年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-14 20:41