<?php

namespace Modules\Post\Models;

use Modules\Main\Models\City;
use Modules\Post\Models\PostCity;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasMany;

use Modules\Main\Models\Scopes\CityScope;

class Post extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'short_text',
        'text',
        'category',
        'image'
    ];

    protected static function booted(): void
    {
        static::addGlobalScope(new CityScope);
    }

    public function cities(): HasManyThrough
    {
        return $this->hasManyThrough(
            City::class, 
            PostCity::class,
            'post_id',
            'id',
            'id',
            'city_id'
            );
    }
}
