<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\EpisodesCategory.
 *
 * @property int                             $id
 * @property string                          $name       名称
 * @property int                             $level      分类等级
 * @property int                             $pid        父id
 * @property string|null                     $path       路径
 * @property int                             $sort       排序越大越靠前
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 *
 * @method static \Illuminate\Database\Eloquent\Builder|EpisodesCategory newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|EpisodesCategory newQuery()
 * @method static \Illuminate\Database\Query\Builder|EpisodesCategory onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|EpisodesCategory query()
 * @method static \Illuminate\Database\Eloquent\Builder|EpisodesCategory whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EpisodesCategory whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EpisodesCategory whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EpisodesCategory whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EpisodesCategory wherePath($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EpisodesCategory wherePid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EpisodesCategory whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EpisodesCategory whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|EpisodesCategory withTrashed()
 * @method static \Illuminate\Database\Query\Builder|EpisodesCategory withoutTrashed()
 *
 * @mixin \Eloquent
 *
 * @property EpisodesCategory|null                                       $parent
 * @property \App\Models\Episode|null                                    $episodes
 * @property \Illuminate\Database\Eloquent\Collection|EpisodesCategory[] $child
 * @property int|null                                                    $child_count
 * @property int|null                                                    $episodes_count
 *
 * @method static \Illuminate\Database\Eloquent\Builder|EpisodesCategory whereLevel($value)
 */
class EpisodesCategory extends Model
{
    use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'episodes_category';

    protected $hidden = [
        'updated_at', 'deleted_at', 'path',
    ];

    public function parent()
    {
        return $this->belongsTo(EpisodesCategory::class, 'pid', 'id');
    }

    public function child()
    {
        return $this->hasMany(EpisodesCategory::class, 'pid', 'id');
    }

    public function episodes()
    {
        return $this->hasMany(Episode::class, 'category_id', 'id');
    }
}
