<?php

namespace App\Models;

use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

class Request extends Model
{
    protected $table = 'requests';
    protected $fillable = ['user_id', 'order_id', 'current_state', 'work_type'];

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

    public function getCreatedAtAttribute($value)
    {
        return Carbon::parse($value)->format('d-m-Y h:i:s');
    }
    public function getUpdatedAtAttribute($value)
    {
        $created = \Carbon\Carbon::createFromFormat('d-m-Y h:i:s',
            Carbon::parse(Carbon::parse($this->created_at)->format('d-m-Y h:i:s'))->format('d-m-Y h:i:s')) ;
        $updated =Carbon::createFromFormat('d-m-Y h:i:s', Carbon::parse($value)->format('d-m-Y h:i:s')) ;
        if($created->eq($updated)) return null;
        return Carbon::parse($value)->format('d-m-Y h:i:s');
    }
}
