<?php

namespace App\Models;

use App\Traits\GlobalStatus;
use Illuminate\Database\Eloquent\Model;

class Department extends Model
{
    use GlobalStatus;

    public function admins()
    {
        return $this->belongsToMany(Admin::class, 'admin_department', 'department_id', 'admin_id');
    }

    public function createdBy()
    {
        return $this->belongsTo(Admin::class, 'admin_id');
    }

    public function form()
    {
        return $this->hasOne(Form::class);
    }

    public function scopeAdminAccess($query)
    {
        return $query->whereHas('admins', function($admin){
            $admin->where('admins.id', auth('admin')->id());
        });
    }
}
