1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
trait SelectOptionsFromModel |
9
|
|
|
{ |
10
|
|
|
protected $optionsLabelAttribute; |
11
|
|
|
|
12
|
|
|
protected $optionsValueAttribute; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* 获取 options 标题字段 |
16
|
|
|
* |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
public function getOptionsLabelAttribute() |
20
|
|
|
{ |
21
|
|
|
return $this->optionsLabelAttribute; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* 设置 options 标题字段 |
26
|
|
|
* |
27
|
|
|
* @param string $value |
28
|
|
|
* |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
|
|
public function setOptionsLabelAttribute($value) |
32
|
|
|
{ |
33
|
|
|
$this->optionsLabelAttribute = $value; |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* 获取 options value 字段 |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getOptionsValueAttribute() |
44
|
|
|
{ |
45
|
|
|
return $this->optionsValueAttribute; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* 设置 options value 字段 |
50
|
|
|
* |
51
|
|
|
* @param string $value |
52
|
|
|
* |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function setOptionsValueAttribute($value) |
56
|
|
|
{ |
57
|
|
|
$this->optionsValueAttribute = $value; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return Model |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function getOptionsModel() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$model = $this->options; |
|
|
|
|
68
|
|
|
|
69
|
|
|
if (is_string($model)) { |
70
|
|
|
$model = app($model); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (!($model instanceof Model)) { |
74
|
|
|
throw new InvalidArgumentException( |
75
|
|
|
sprintf( |
76
|
|
|
'The select options class must be instanced of "%s".', |
77
|
|
|
Model::class |
78
|
|
|
) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $model; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
protected function setOptionsFromModel() |
90
|
|
|
{ |
91
|
|
|
if (is_null(($label = $this->getOptionsLabelAttribute()))) { |
92
|
|
|
throw new InvalidArgumentException('The select options must set label attribute'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$model = $this->getOptionsModel(); |
96
|
|
|
|
97
|
|
|
// $repository = app(RepositoryInterface::class); |
|
|
|
|
98
|
|
|
// $repository->setModel($model); |
|
|
|
|
99
|
|
|
|
100
|
|
|
// $results = $repository->getQuery()->get(); |
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var \Illuminate\Support\Collection $results |
103
|
|
|
*/ |
104
|
|
|
$results = $model->get(); |
105
|
|
|
|
106
|
|
|
$key = $this->getOptionsValueAttribute() ?: $model->getKeyName(); |
107
|
|
|
|
108
|
|
|
return $results->pluck($label, $key); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.