176 lines
4.4 KiB
PHP
176 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Locations;
|
|
|
|
use App\Models\Bank;
|
|
use App\Models\Category;
|
|
use App\Models\Locations\DistrictLocale;
|
|
use App\Models\Organization;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
class District extends Model
|
|
{
|
|
/**
|
|
* The database table doesn't use 'created_at' and 'updated_at' so we disable it from Inserts/Updates.
|
|
*
|
|
* @var bool
|
|
*/
|
|
public $timestamps = false;
|
|
|
|
|
|
/**
|
|
* Return all available locales of the district.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function translations()
|
|
{
|
|
return $this->hasMany(DistrictLocale::class, 'district_id');
|
|
}
|
|
|
|
|
|
/**
|
|
* Get the local district name.
|
|
* In the App::getLocale, or if not exists, in the App::getFallbackLocale language.
|
|
* @return void
|
|
*/
|
|
public function name()
|
|
{
|
|
$result = $this->hasMany(DistrictLocale::class, 'district_id')
|
|
->where('locale', App::getLocale());
|
|
if ($result->count() === 0) {
|
|
$result = $this->hasMany(DistrictLocale::class, 'district_id')
|
|
->where('locale', App::getFallbackLocale());
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* Get all of the users of the districts.
|
|
* Many-to-many polymorphic.
|
|
* @return void
|
|
*/
|
|
public function users()
|
|
{
|
|
return $this->morphedByMany(User::class, 'districtable', 'districtables');
|
|
}
|
|
|
|
|
|
/**
|
|
* Get all of the organizations of the districts.
|
|
* Many-to-many polymorphic.
|
|
* @return void
|
|
*/
|
|
public function organizations()
|
|
{
|
|
return $this->morphedByMany(Organization::class, 'districtable', 'districtables');
|
|
}
|
|
|
|
|
|
/**
|
|
* Get all of the banks of the districts.
|
|
* Many-to-many polymorphic.
|
|
* @return void
|
|
*/
|
|
public function banks()
|
|
{
|
|
return $this->morphedByMany(Bank::class, 'districtable', 'districtables');
|
|
}
|
|
|
|
|
|
/**
|
|
* Get all related locations of the division.
|
|
* One-to-many.
|
|
* @return void
|
|
*/
|
|
public function locations()
|
|
{
|
|
return $this->hasMany(Location::class);
|
|
}
|
|
|
|
|
|
/**
|
|
* Get the related city of the district.
|
|
* In the App::getLocale, or if not exists, in the App::getFallbackLocale language.
|
|
* @return void
|
|
*/
|
|
public function city()
|
|
{
|
|
$city = $this->belongsTo(City::class, 'city_id')->pluck('id');
|
|
$result = CityLocale::where('city_id', $city)
|
|
->where('locale', App::getLocale());
|
|
if ($result->count() === 0) {
|
|
$result = CityLocale::where('city_id', $city)
|
|
->where('locale', App::getFallbackLocale());
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Get the related country of the district.
|
|
* In the App::getLocale, or if not exists, in the App::getFallbackLocale language.
|
|
* @return void
|
|
*/
|
|
public function country()
|
|
{
|
|
$country = $this->belongsTo(City::class, 'city_id')->pluck('country_id');
|
|
$result = CountryLocale::where('country_id', $country)
|
|
->where('locale', App::getLocale());
|
|
if ($result->count() === 0) {
|
|
$result = CountryLocale::where('country_id', $country)
|
|
->where('locale', App::getFallbackLocale());
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* Get the related division of the district.
|
|
* In the App::getLocale, or if not exists, in the App::getFallbackLocale language.
|
|
* @return void
|
|
*/
|
|
public function division()
|
|
{
|
|
$division = $this->belongsTo(City::class, 'city_id')->pluck('division_id');
|
|
$result = DivisionLocale::where('division_id', $division)
|
|
->where('locale', App::getLocale());
|
|
if ($result->count() === 0) {
|
|
$result = DivisionLocale::where('division_id', $division)
|
|
->where('locale', App::getFallbackLocale());
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* Get the related parent of this model.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function parent()
|
|
{
|
|
// Check if division exists
|
|
if ($this->division_id) {
|
|
return $this->belongsTo(Division::class, 'division_id');
|
|
}
|
|
|
|
// Otherwise, return the country relationship
|
|
return $this->belongsTo(Country::class, 'country_id');
|
|
}
|
|
|
|
|
|
/**
|
|
* Get all of the related categories for this model.
|
|
*/
|
|
public function categories()
|
|
{
|
|
return $this->morphMany(Category::class, 'categoryable');
|
|
}
|
|
|
|
}
|