106 lines
2.7 KiB
PHP
106 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
class City extends Model
|
|
{
|
|
use HasFactory;
|
|
/**
|
|
* The attributes that should be hidden for arrays.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $hidden = [
|
|
'name_en',
|
|
'feature_id',
|
|
'province_feature_id',
|
|
'center_lat',
|
|
'center_long',
|
|
'created_at',
|
|
'updated_at'
|
|
];
|
|
|
|
protected $fillable = [
|
|
'geometry',
|
|
];
|
|
|
|
public function province(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->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;
|
|
}
|
|
}
|