-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDriverWorkTime.php
119 lines (100 loc) · 2.61 KB
/
DriverWorkTime.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace App\Models;
use Eloquent as Model;
/**
* Class DriverWorkTime
* @package App\Models
* @version February 28, 2022, 10:08 am EET
*
* @property integer user_id
* @property integer created_by_id
* @property integer updated_by_id
* @property string|\Carbon\Carbon from_time
* @property string|\Carbon\Carbon to_time
*/
class DriverWorkTime extends Model
{
public $table = 'driver_work_times';
public $fillable = [
'user_id',
'created_by_id',
'updated_by_id',
'from_time',
'to_time'
];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'user_id' => 'integer',
'created_by_id' => 'integer',
'updated_by_id' => 'integer',
'from_time' => 'datetime',
'to_time' => 'datetime'
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
'user_id' => 'required|integer|exists:users:id,name',
'from_time' => 'required',
'to_time' => 'required'
];
/**
* New Attributes
*
* @var array
*/
protected $appends = [
// 'custom_fields',
'time',
];
public function customFieldsValues()
{
return $this->morphMany('App\Models\CustomFieldValue', 'customizable');
}
public function getCustomFieldsAttribute()
{
$hasCustomField = in_array(static::class, setting('custom_field_models', []));
if (!$hasCustomField) {
return [];
}
$array = $this->customFieldsValues()
->join('custom_fields', 'custom_fields.id', '=', 'custom_field_values.custom_field_id')
->where('custom_fields.in_table', '=', true)
->get()->toArray();
return convertToAssoc($array, 'name');
}
public function getTimeAttribute()
{
if ($this->to_time) {
return $this->to_time->diffInSeconds($this->from_time);
}
return '0';
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
**/
public function user()
{
return $this->belongsTo(\App\Models\User::class, 'user_id', 'id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
**/
public function createdBy()
{
return $this->belongsTo(\App\Models\User::class, 'created_by_id', 'id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
**/
public function updatedBy()
{
return $this->belongsTo(\App\Models\User::class, 'updated_by_id', 'id');
}
}