6
0
Fork 0
jiqu-library-server/app/Models/Zone.php

84 lines
1.5 KiB
PHP

<?php
namespace App\Models;
use Dcat\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;
class Zone extends Model
{
use ModelTree;
public const TYPE_PROVINCE = 'province';
public const TYPE_CITY = 'city';
public const TYPE_AREA = 'area';
/**
* @var array
*/
protected $fillable = [
'name',
'parent_id',
'type',
];
/**
* 仅查询区级地区
*/
public function scopeAreas($query)
{
return $query->where('type', static::TYPE_AREA);
}
/**
* 此地区的上级地区
*/
public function parent()
{
return $this->belongsTo(static::class, 'parent_id');
}
/**
* 确认此地区是否是省
*
* @return bool
*/
public function isProvince(): bool
{
return $this->type === static::TYPE_PROVINCE;
}
/**
* 确认此地区是否是市
*
* @return bool
*/
public function isCity(): bool
{
return $this->type === static::TYPE_CITY;
}
/**
* 确认此地区是否是区
*
* @return bool
*/
public function isArea(): bool
{
return $this->type === static::TYPE_AREA;
}
/**
* 获取当前地区的完整名称
*
* @return string
*/
public function getFullNameAttribute()
{
if ($this->parent_id === null) {
return $this->name;
}
return $this->parent->full_name . ' ' . $this->name;
}
}