<?php

namespace App;

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

use App\Traits\ModelLogChanging;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;

class OwnerMaterial extends Model
{
    use ModelLogChanging;
    use ModelTimeFormat;
    protected $table = 'owner_material';
    protected $fillable = ['owner_id','material_id','material_code','size','special','specification','initiator'];

    public function owner():BelongsTo
    {
        return $this->belongsTo(Owner::class);
    }

    public function material():BelongsTo
    {
        return $this->belongsTo(Material::class);
    }

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

    public function file():HasOne
    {
        return $this->hasOne(UploadFile::class,'table_id','id')->where('table_name','owner_material');
    }
    public function getSpecificationAttribute($value){
        return str_replace(array("\r\n","\n","\r","\"","&quot;"),' ',$value);
    }

    public function scopeFilter($query,$filters)
    {
        return $filters->apply($query);
    }
}
