<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;

/**
 * @method static orderBy(string $string, string $string1)
 */use App\Traits\ModelTimeFormat;

class Owner extends Model
{
    use ModelTimeFormat;
    use SoftDeletes;
    public $fillable = ['name','code','checking_count'];
    public static function filterAuthorities(){
        $user=Auth::user();
        if(!$user){
            return (new static)->newQuery()->where('id','0');
        }
        $ownerIds=$user->getPermittingOwnerIdsAttribute();
        return (new static)->newQuery()->whereIn('id',$ownerIds);
    }
    public function getIncreasedCheckingCount(){
        $this['checking_count']=$this['checking_count']+1;
        $this->update();
        return $this['checking_count'];
    }

    public function paperBoxes()
    {
        return $this->belongsToMany('\App\PaperBox', 'owner_paper_box', 'owner_id', 'paper_box_id');
    }
}
