<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends BaseModel
{
    protected $table = 'categories';

    protected $appends = ['posterCount'];

    protected $fillable = ['id', 'name', 'slug', 'parent_id', 'address', 'description', 'start_date', 'position', 'user_id', 'is_show_bbs', 'is_show_column', 'is_activated', 'is_deleted', 'created_at', 'updated_at'];

    public function parent() {
        return $this->belongsTo(Category::class, 'parent_id', 'id')->with('parent')->select('id', 'name', 'slug', 'parent_id')->where('is_deleted', false)->where('is_activated', true);
    }

    public function user() {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function item() {
        return $this->hasMany(Category::class, 'parent_id', 'id')->select('id', 'name', 'slug', 'parent_id')->where('is_deleted', false)->where('is_activated', true);
    }

    public function workers() {
        return $this->hasMany(ProjectUser::class, 'category_id', 'id')->with('address_list', 'user');
    }

    public function poster()
    {
        return $this->hasMany(Photo::class, 'project_id', 'id');
    }

    public function getPosterCountAttribute() {
        return $this->poster()->count();
    }
}
