-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathEntryModel.php
More file actions
70 lines (57 loc) · 1.73 KB
/
EntryModel.php
File metadata and controls
70 lines (57 loc) · 1.73 KB
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
<?php
namespace Statamic\Eloquent\Entries;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Statamic\Eloquent\Database\BaseModel;
class EntryModel extends BaseModel
{
protected $guarded = [];
protected $table = 'entries';
protected function casts(): array
{
return [
'data' => 'json',
'published' => 'boolean',
];
}
public function date(): Attribute
{
return Attribute::make(
get: function ($value) {
return Carbon::parse($value, 'UTC');
},
set: function ($value) {
if (! $value instanceof Carbon) {
$value = Carbon::parse($value, 'UTC');
}
if ($value->tzName !== 'UTC') {
$value = $value->utc();
}
return $value->format('Y-m-d H:i:s');
},
);
}
public function author()
{
return $this->belongsTo(\App\Models\User::class, 'data->author');
}
public function origin()
{
return $this->belongsTo(static::class);
}
public function parent()
{
return $this->belongsTo(static::class, 'data->parent');
}
public function getAttribute($key)
{
// Because the import script was importing `updated_at` into the
// json data column, we will explicitly reference other SQL
// columns first to prevent errors with that bad data.
if (in_array($key, EntryQueryBuilder::COLUMNS)) {
return parent::getAttribute($key);
}
return Arr::get($this->getAttributeValue('data'), $key, parent::getAttribute($key));
}
}