belongsTo('App\Models\Province'); } public function users() { return $this->hasMany('App\Models\User'); } /** * Get all of the projects for the city. */ public function roadConstructionProjects() { return $this->hasManyThrough('App\Models\RoadConstructionProject', 'App\Models\User'); } /** * Get all of the projects for the city. */ public function roadItemsProjects() { return $this->hasManyThrough('App\Models\RoadItemsProject', 'App\Models\User'); } /** * Get all of the projects for the city. */ public function roadPatrolProjects() { return $this->hasManyThrough('App\Models\RoadPatrolProject', 'App\Models\User'); } /** * Get all of the projects for the province. */ public function roadMaintenanceProjects() { return $this->hasMany('App\Models\RoadMaintenanceProject'); } /** * Get all of the projects for the province. */ public function roadConstructionRural() { return $this->hasMany('App\Models\RoadConstructionRural'); } public function edarateShahri(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { return $this->belongsToMany('App\Models\EdarateShahri', 'edarate_shahri_be_city')->withPivot('is_primary','id'); } public static function findCityWithLatLng($lat, $lng): Model|null { try { $city = City::query() ->whereRaw("ST_Contains( geometry, ST_GeomFromText('POINT($lng $lat)', 4326))" ) ->firstOrFail(['id', 'name_fa', 'province_id']); } catch (ModelNotFoundException $exception) { $city = City::query() ->orderByRaw("ST_Distance( geometry, ST_GeomFromText('POINT($lng $lat)', 4326)) ASC" ) ->first(['id', 'name_fa', 'province_id']); } return $city; } }