1
|
|
|
@php echo "<?php" |
|
|
|
|
2
|
|
|
@endphp |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace {{ $modelNameSpace }}; |
6
|
|
|
@php |
7
|
|
|
$hasRoles = false; |
8
|
|
|
if(count($relations) && isset($relations["belongsToMany"]) && count($relations['belongsToMany'])) { |
9
|
|
|
$hasRoles = $relations['belongsToMany']->filter(function($belongsToMany) { |
10
|
|
|
return $belongsToMany['related_table'] == 'roles'; |
11
|
|
|
})->count() > 0; |
12
|
|
|
$relations['belongsToMany'] = $relations['belongsToMany']->reject(function($belongsToMany) { |
13
|
|
|
return $belongsToMany['related_table'] == 'roles'; |
14
|
|
|
}); |
15
|
|
|
} |
16
|
|
|
@endphp |
17
|
|
|
/* Imports */ |
18
|
|
|
use DateTimeInterface; |
19
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
20
|
|
|
use Illuminate\Database\Eloquent\Model; |
21
|
|
|
@if($hasSoftDelete)use Illuminate\Database\Eloquent\SoftDeletes; |
22
|
|
|
@endif |
23
|
|
|
@if (isset($relations['belongsToMany']) && count($relations['belongsToMany'])) |
24
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
25
|
|
|
@endif |
26
|
|
|
@if($hasRoles)use Spatie\Permission\Traits\HasRoles; |
27
|
|
|
@endif |
28
|
|
|
|
29
|
|
|
class {{ $modelBaseName }} extends Model |
30
|
|
|
{ |
31
|
|
|
@if($hasSoftDelete) |
32
|
|
|
use SoftDeletes; |
33
|
|
|
@endif |
34
|
|
|
@if($hasRoles)use HasRoles; |
35
|
|
|
@endif |
36
|
|
|
use HasFactory; |
37
|
|
|
@if (!is_null($tableName))protected $table = '{{ $tableName }}'; |
38
|
|
|
@endif |
39
|
|
|
@if ($fillable) |
40
|
|
|
|
41
|
|
|
protected $fillable = [ |
42
|
|
|
@foreach($fillable as $f) |
43
|
|
|
'{{ $f }}', |
44
|
|
|
@endforeach |
45
|
|
|
|
46
|
|
|
]; |
47
|
|
|
@endif |
48
|
|
|
|
49
|
|
|
@if ($hidden && count($hidden) > 0)protected $hidden = [ |
50
|
|
|
@foreach($hidden as $h) |
51
|
|
|
'{{ $h }}', |
52
|
|
|
@endforeach |
53
|
|
|
|
54
|
|
|
]; |
55
|
|
|
@endif |
56
|
|
|
|
57
|
|
|
@if ($booleans && count($booleans) > 0)protected $casts = [ |
58
|
|
|
@foreach($booleans as $b) |
59
|
|
|
'{{ $b }}' => 'boolean', |
60
|
|
|
@endforeach |
61
|
|
|
|
62
|
|
|
]; |
63
|
|
|
@endif |
64
|
|
|
|
65
|
|
|
protected $dates = [ |
66
|
|
|
@if ($dates) |
67
|
|
|
@foreach($dates as $date) |
68
|
|
|
'{{ $date }}' => 'Y-m-d', |
69
|
|
|
@endforeach |
70
|
|
|
@endif |
71
|
|
|
@if($datetimes) |
72
|
|
|
@foreach($datetimes as $date) |
73
|
|
|
'{{ $date }}', |
74
|
|
|
@endforeach |
75
|
|
|
@endif |
76
|
|
|
]; |
77
|
|
|
@if (!$timestamps)public $timestamps = false; |
78
|
|
|
@endif |
79
|
|
|
|
80
|
|
|
protected $appends = ["api_route", "can"]; |
81
|
|
|
|
82
|
|
|
/* ************************ ACCESSOR ************************* */ |
83
|
|
|
|
84
|
|
|
public function getApiRouteAttribute() { |
85
|
|
|
return route("api.{{ $routeBaseName }}.index"); |
86
|
|
|
} |
87
|
|
|
public function getCanAttribute() { |
88
|
|
|
return [ |
89
|
|
|
"view" => \Auth::check() && \Auth::user()->can("view", $this), |
90
|
|
|
"update" => \Auth::check() && \Auth::user()->can("update", $this), |
91
|
|
|
"delete" => \Auth::check() && \Auth::user()->can("delete", $this), |
92
|
|
|
"restore" => \Auth::check() && \Auth::user()->can("restore", $this), |
93
|
|
|
"forceDelete" => \Auth::check() && \Auth::user()->can("forceDelete", $this), |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function serializeDate(DateTimeInterface $date) { |
98
|
|
|
return $date->format('Y-m-d H:i:s'); |
99
|
|
|
} |
100
|
|
|
@if (count($relations)) |
101
|
|
|
|
102
|
|
|
/* ************************ RELATIONS ************************ */ |
103
|
|
|
@if(isset($relations['belongsTo']) && count($relations['belongsTo'])) |
104
|
|
|
@foreach($relations["belongsTo"] as $belongsTo) |
105
|
|
|
/** |
106
|
|
|
* Many to One Relationship to {{$belongsTo["related_model"]}} |
107
|
|
|
* {{'@'}}return \Illuminate\Database\Eloquent\Relations\BelongsTo |
108
|
|
|
*/ |
109
|
|
|
public function {{$belongsTo['function_name']}}() { |
110
|
|
|
return $this->belongsTo({{$belongsTo['related_model']}},"{{$belongsTo['foreign_key']}}","{{$belongsTo["owner_key"]}}"); |
111
|
|
|
} |
112
|
|
|
@endforeach |
113
|
|
|
@endif |
114
|
|
|
@if (isset($relations["belongsToMany"]) && count($relations['belongsToMany'])) |
115
|
|
|
@foreach($relations['belongsToMany'] as $belongsToMany) |
116
|
|
|
/** |
117
|
|
|
* Relation to {{ $belongsToMany['related_model_name_plural'] }} |
118
|
|
|
* |
119
|
|
|
* {{'@'}}return BelongsToMany |
120
|
|
|
*/ |
121
|
|
|
public function {{ Str::camel($belongsToMany['related_table']) }}() { |
122
|
|
|
return $this->belongsToMany({{ $belongsToMany['related_model_class'] }}, '{{ $belongsToMany['relation_table'] }}', '{{ $belongsToMany['foreign_key'] }}', '{{ $belongsToMany['related_key'] }}'); |
123
|
|
|
} |
124
|
|
|
@endforeach |
125
|
|
|
|
126
|
|
|
@endif |
127
|
|
|
@endif} |
128
|
|
|
|