MorphMapTrait.php
3.9 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 多态关联 trait
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Models\Traits
* @package App\Models\Traits
* @author Richer <yangzi1028@163.com>
* @date 2020年7月28日,14:18:25
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Models\Traits;
use App\Models\Chat\ChatRecordItem;
use App\Models\RechargeOrder;
use App\Models\User\Admin;
use App\Models\User\User;
use App\Models\User\WithdrawRecord;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
/**
* Trait MorphMapTrait
*
* @category App\Models\Traits
* @package App\Models\Traits
* @author Richer <yangzi1028@163.com>
* @date 2020年7月28日,14:18:25
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
trait MorphMapTrait
{
public static function boot()
{
parent::boot();
self::bootEloquentMorphs();
}
/**
* 自定义多态关联的类型字段
*/
private static function bootEloquentMorphs()
{
Relation::morphMap(self::getMorphTypeOptions());
}
protected static function initModel()
{
// 设置模型
return [
];
}
/**
* 获取 type options
*
* @return array
*/
public static function getMorphTypeOptions()
{
return [
User::OBJ_NAME => User::class,
RechargeOrder::OBJ_NAME => RechargeOrder::class,
ChatRecordItem::OBJ_NAME => ChatRecordItem::class,
WithdrawRecord::OBJ_NAME => WithdrawRecord::class,
];
$classes = self::initModel();
$rs = [];
if ($classes) {
foreach ($classes as $class) {
$rs [$class::OBJ_NAME] = $class::OBJ_NAME_ZH;
}
}
return $rs;
return [
User::OBJ_NAME => User::class,
];
}
/**
* 获取 type options
*
* @return array
*/
public static function getMorphTypeZHOptions()
{
return [
User::OBJ_NAME => User::OBJ_NAME_ZH,
RechargeOrder::OBJ_NAME => RechargeOrder::OBJ_NAME_ZH,
ChatRecordItem::OBJ_NAME => ChatRecordItem::OBJ_NAME_ZH,
WithdrawRecord::OBJ_NAME => ChatRecordItem::OBJ_NAME_ZH,
];
$classes = self::initModel();
$rs = [];
if ($classes) {
foreach ($classes as $class) {
$rs [$class::OBJ_NAME] = $class::OBJ_NAME_ZH;
}
}
return $rs;
return [
User::OBJ_NAME => User::OBJ_NAME_ZH,
];
}
/**
* 获取多态关联的类
*
* @param $class
* @return string
*/
public static function getMorphToTypes($class)
{
return Arr::get(array_flip(self::getMorphTypeOptions()), $class);
}
/**
* 获取多态关联的类
*
* @param string $type
* @return string
*/
public static function getMorphToClass($type = '')
{
return Arr::get(self::getMorphTypeOptions(), $type);
}
/**
* 获取多态关联的类数组
*
* @return array
*/
public static function getMorphToClasses()
{
return array_values(self::getMorphTypeOptions());
}
/**
* 获取 type options
*
* @param $type
* @param null $default
* @return array
*/
public static function getMorphTypeZH($type, $default = null)
{
return Arr::get(self::getMorphTypeZHOptions(), $type, $default);
}
}