<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Setting extends Model
{
    use HasFactory;
    public function scopeNearby($query, $latitude, $longitude, $distance = 0.5) // Distance in kilometers
    {
        $haversine = "(6371 * acos(cos(radians($latitude)) * cos(radians(latitude)) * cos(radians(longitude) - radians($longitude)) + sin(radians($latitude)) * sin(radians(latitude))))";
                        
        return $query->select('*')
                     ->selectRaw("$haversine AS distance")
                     ->having("distance", "<=", $distance);
    }

}
