-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.blade.php
More file actions
122 lines (107 loc) · 2.83 KB
/
Copy pathModel.blade.php
File metadata and controls
122 lines (107 loc) · 2.83 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
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
120
121
122
{{--
Model.blade.php
Blade-шаблон для генерации модели PET Framework.
Используется командой: php pet make:model ModelName
Переменные:
- $namespace — пространство имён (App\Model)
- $className — имя класса (User)
- $table — имя таблицы (users)
- $fillable — массив полей для fillable
- $hidden — массив скрытых полей
- $casts — массив кастов полей
- $timestamps — использовать ли timestamps (bool)
- $connection — подключение к БД (null|string)
--}}
<?php
namespace {{ $namespace }};
use Pet\Model\Model;
class {{ $className }} extends Model
{
protected string $table = '{{ $table }}';
@if($connection)
protected static ?string $connection = '{{ $connection }}';
@endif
@if(!empty($fillable))
protected array $fillable = [
@foreach($fillable as $field)
'{{ $field }}',
@endforeach
];
@endif
@if(!empty($hidden))
public array $hidden = [
@foreach($hidden as $field)
'{{ $field }}',
@endforeach
];
@endif
@if(!empty($casts))
protected array $casts = [
@foreach($casts as $field => $type)
'{{ $field }}' => '{{ $type }}',
@endforeach
];
@endif
@if($timestamps)
public bool $timestamps = true;
@else
public bool $timestamps = false;
@endif
/**
* Получить все записи.
*
* @return array
*/
public static function all(): array
{
$instance = new static();
return $instance->find();
}
/**
* Найти запись по ID.
*
* @param int $id
* @return static|null
*/
public static function find(int $id): ?static
{
$instance = new static();
$result = $instance->find(['id' => $id]);
return !empty($result) ? $result[0] : null;
}
/**
* Создать новую запись.
*
* @param array $data
* @return static|null
*/
public static function create(array $data): ?static
{
$instance = new static();
$id = $instance->create($data);
return $id ? static::find($id) : null;
}
/**
* Обновить запись.
*
* @param int $id
* @param array $data
* @return bool
*/
public static function update(int $id, array $data): bool
{
$instance = new static();
return $instance->edit($id, $data);
}
/**
* Удалить запись.
*
* @param int $id
* @return bool
*/
public static function delete(int $id): bool
{
$instance = new static();
return $instance->remove($id);
}
}