File: /var/www/limestate-admin/app/Models/Complex.php
<?php
namespace App\Models;
use App\Collections\BuildingCollection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Orchid\Attachment\Attachable;
use Orchid\Attachment\Models\Attachment;
use Orchid\Filters\Filterable;
use Orchid\Filters\Types\Like;
use Orchid\Filters\Types\Where;
use Orchid\Screen\AsSource;
/**
* @property int $id
* @property string $name
* @property string $alias
* @property ?string $description
* @property ?string $beautification
* @property ?string $infrastructure
* @property ?int $region_id
* @property ?int $region_district_id
* @property ?int $city_district_id
* @property ?int $metro_id
* @property ?int $city_id
* @property ?string $street_name
* @property ?int $started_year
* @property ?int $started_quarter
* @property ?int $completed_year
* @property ?int $completed_quarter
* @property ?int $is_completed
* @property ?int $developer_id
* @property ?string $latlng
* @property ?string $is_special
* @property bool $is_apartment
* @property ?string $profitability_1
* @property ?string $profitability_2
* @property ?string $profitability_3
* @property ?string $profitability_4
* @property ?string $profitability_5
* @property ?string $terms_of_purchase
* @property ?string $special_offer
* @property ?string $schema_coords
* @property ?int $old_ID
* @property ?int $published
* @property ?string $meta_title
* @property ?string $meta_description
* @property ?string $meta_keywords
* @property ?string $created_at
* @property ?string $updated_at
* @property ?int $category_id
* @property ?int $plan_id
* @property ?string $deleted_at
*
* @method MorphToMany[] attachment()
* @property Attachment[] $attachment
*
* @property BuildingCollection $buildings
* @property ?ExternalServiceRelation $yandexRelation
* @property Attachment $plan
*/
class Complex extends Model
{
use HasFactory;
use AsSource;
use Filterable;
use Attachable;
use SoftDeletes;
protected $fillable = [
'name',
'alias',
'description',
'beautification',
'infrastructure',
'region_id',
'region_district_id',
'city_district_id',
'metro_id',
'city_id',
'street_name',
'started_year',
'started_quarter',
'completed_year',
'completed_quarter',
'is_completed',
'developer_id',
'latlng',
'is_special',
'is_apartment',
'profitability_1',
'profitability_2',
'profitability_3',
'profitability_4',
'profitability_5',
'terms_of_purchase',
'special_offer',
'schema_coords',
'old_ID',
'published',
'meta_title',
'meta_description',
'meta_keywords',
'category_id',
'plan_id',
];
protected array $allowedSorts = ['id', 'updated_at', 'published'];
protected array $allowedFilters = [
'name' => Like::class,
'published' => Where::class,
'category_id' => Where::class,
];
public function setDescriptionAttribute($newValue): void
{
$this->attributes['description'] = $newValue ?? '';
}
public function setBeautificationAttribute($newValue): void
{
$this->attributes['beautification'] = $newValue ?? '';
}
public function setInfrastructureAttribute($newValue): void
{
$this->attributes['infrastructure'] = $newValue ?? '';
}
public function getFlatsCount(): int
{
$sum = 0;
foreach ($this->buildings as $building) {
$sum += $building->flats->count();
}
return $sum;
}
public function getYandexId(): ?string
{
return $this->yandexRelation?->external_id;
}
// MARK: - Relations
public function buildings(): HasMany
{
return $this->hasMany(Building::class)->where('published', true);
}
public function yandexRelation(): HasOne
{
return $this->hasOne(ExternalServiceRelation::class, 'model_id')
->where('service', ExternalServiceRelation::SERVICE_YANDEX)
->where('model', ExternalServiceRelation::MODEL_COMPLEX);
}
public function plan(): BelongsTo
{
return $this->belongsTo(Attachment::class, 'plan_id');
}
}