app/Statistics/Repository/FamilyRepository.php
private function ageBetweenSpousesQuery(string $age_dir, int $total): array
    {
        $prefix = DB::connection()->getTablePrefix();

        $query = DB::table('families')
            ->where('f_file', '=', $this->tree->id())
            ->join('dates AS wife', static function (JoinClause $join): void {
                $join
                    ->on('wife.d_gid', '=', 'f_wife')
                    ->on('wife.d_file', '=', 'f_file')
                    ->where('wife.d_fact', '=', 'BIRT')
                    ->where('wife.d_julianday1', '<>', 0);
            })
            ->join('dates AS husb', static function (JoinClause $join): void {
                $join
                    ->on('husb.d_gid', '=', 'f_husb')
                    ->on('husb.d_file', '=', 'f_file')
                    ->where('husb.d_fact', '=', 'BIRT')
                    ->where('husb.d_julianday1', '<>', 0);
            });

        if ($age_dir === 'DESC') {
            $query
                ->whereColumn('wife.d_julianday1', '>=', 'husb.d_julianday1')
                ->orderBy(DB::raw('MIN(' . $prefix . 'wife.d_julianday1) - MIN(' . $prefix . 'husb.d_julianday1)'), 'DESC');
        } else {
            $query
                ->whereColumn('husb.d_julianday1', '>=', 'wife.d_julianday1')
                ->orderBy(DB::raw('MIN(' . $prefix . 'husb.d_julianday1) - MIN(' . $prefix . 'wife.d_julianday1)'), 'DESC');
        }

        return $query
            ->groupBy(['f_id', 'f_file'])
            ->select('families.*')
            ->take($total)
            ->get()
            ->map(Family::rowMapper())
            ->filter(GedcomRecord::accessFilter())
            ->map(function (Family $family) use ($age_dir): array {
                $husb_birt_jd = $family->husband()->getBirthDate()->minimumJulianDay();
                $wife_birt_jd = $family->wife()->getBirthDate()->minimumJulianDay();

                if ($age_dir === 'DESC') {
                    $diff = $wife_birt_jd - $husb_birt_jd;
                } else {
                    $diff = $husb_birt_jd - $wife_birt_jd;
                }

                return [
                    'family' => $family,
                    'age'    => $this->calculateAge($diff),
                ];
            })
            ->all();
    }
private function ageBetweenSpousesQuery(string $age_dir, int $total): array
    {
        $prefix = DB::connection()->getTablePrefix();

        $query = DB::table('families')
            ->where('f_file', '=', $this->tree->id())
            ->join('dates AS wife', static function (JoinClause $join): void {
                $join
                    ->on('wife.d_gid', '=', 'f_wife')
                    ->on('wife.d_file', '=', 'f_file')
                    ->where('wife.d_fact', '=', 'BIRT')
                    ->where('wife.d_julianday1', '<>', 0);
            })
            ->join('dates AS husb', static function (JoinClause $join): void {
                $join
                    ->on('husb.d_gid', '=', 'f_husb')
                    ->on('husb.d_file', '=', 'f_file')
                    ->where('husb.d_fact', '=', 'BIRT')
                    ->where('husb.d_julianday1', '<>', 0);
            });

        if ($age_dir === 'DESC') {
            $query
                ->whereColumn('wife.d_julianday1', '>=', 'husb.d_julianday1')
                ->orderBy(DB::raw('MIN(' . $prefix . 'wife.d_julianday1) - MIN(' . $prefix . 'husb.d_julianday1)'), 'DESC');
        } else {
            $query
                ->whereColumn('husb.d_julianday1', '>=', 'wife.d_julianday1')
                ->orderBy(DB::raw('MIN(' . $prefix . 'husb.d_julianday1) - MIN(' . $prefix . 'wife.d_julianday1)'), 'DESC');
        }

        return $query
            ->groupBy(['f_id', 'f_file'])
            ->select('families.*')
            ->take($total)
            ->get()
            ->map(Family::rowMapper())
            ->filter(GedcomRecord::accessFilter())
            ->map(function (Family $family) use ($age_dir): array {
                $husb_birt_jd = $family->husband()->getBirthDate()->minimumJulianDay();
                $wife_birt_jd = $family->wife()->getBirthDate()->minimumJulianDay();

                if ($age_dir === 'DESC') {
                    $diff = $wife_birt_jd - $husb_birt_jd;
                } else {
                    $diff = $husb_birt_jd - $wife_birt_jd;
                }

                return [
                    'family' => $family,
                    'age'    => $this->calculateAge($diff),
                ];
            })
            ->all();
    }
private function ageBetweenSpousesQuery ( string $ age_dir , int $ total ) : array { $ prefix = DB :: connection ( ) -> getTablePrefix ( ) ; $ query = DB :: table ( 'families' ) -> where ( 'f_file' , '=' , $ this -> tree -> id ( ) ) -> join ( 'dates AS wife' , static function ( JoinClause $ join ) : void { $ join -> on ( 'wife.d_gid' , '=' , 'f_wife' ) -> on ( 'wife.d_file' , '=' , 'f_file' ) -> where ( 'wife.d_fact' , '=' , 'BIRT' ) -> where ( 'wife.d_julianday1' , '<>' , 0 ) ; } ) -> join ( 'dates AS husb' , static function ( JoinClause $ join ) : void { $ join -> on ( 'husb.d_gid' , '=' , 'f_husb' ) -> on ( 'husb.d_file' , '=' , 'f_file' ) -> where ( 'husb.d_fact' , '=' , 'BIRT' ) -> where ( 'husb.d_julianday1' , '<>' , 0 ) ; } ) ; if ( $ age_dir === 'DESC' ) { $ query -> whereColumn ( 'wife.d_julianday1' , '>=' , 'husb.d_julianday1' ) -> orderBy ( DB :: raw ( 'MIN(' . $ prefix . 'wife.d_julianday1) - MIN(' . $ prefix . 'husb.d_julianday1)' ) , 'DESC' ) ; } else { $ query -> whereColumn ( 'husb.d_julianday1' , '>=' , 'wife.d_julianday1' ) -> orderBy ( DB :: raw ( 'MIN(' . $ prefix . 'husb.d_julianday1) - MIN(' . $ prefix . 'wife.d_julianday1)' ) , 'DESC' ) ; } return $ query -> groupBy ( [ 'f_id' , 'f_file' ] ) -> select ( 'families.*' ) -> take ( $ total ) -> get ( ) -> map ( Family :: rowMapper ( ) ) -> filter ( GedcomRecord :: accessFilter ( ) ) -> map ( function ( Family $ family ) use ( $ age_dir ) : array { $ husb_birt_jd = $ family -> husband ( ) -> getBirthDate ( ) -> minimumJulianDay ( ) ; $ wife_birt_jd = $ family -> wife ( ) -> getBirthDate ( ) -> minimumJulianDay ( ) ; if ( $ age_dir === 'DESC' ) { $ diff = $ wife_birt_jd - $ husb_birt_jd ; } else { $ diff = $ husb_birt_jd - $ wife_birt_jd ; } return [ 'family' => $ family , 'age' => $ this -> calculateAge ( $ diff ) , ] ; } ) -> all ( ) ; }
Find the ages between spouses.

@param string $age_dir
@param int    $total

@return array
Find the ages between spouses .