@@ 201-230 (lines=30) @@ | ||
198 | * |
|
199 | * @return $this |
|
200 | */ |
|
201 | public function model($model, $idField = 'id', $textField = 'name') |
|
202 | { |
|
203 | if (!class_exists($model) |
|
204 | || !in_array(Model::class, class_parents($model)) |
|
205 | ) { |
|
206 | throw new \InvalidArgumentException("[$model] must be a valid model class"); |
|
207 | } |
|
208 | ||
209 | $this->options = function ($value) use ($model, $idField, $textField) { |
|
210 | if (empty($value)) { |
|
211 | return []; |
|
212 | } |
|
213 | ||
214 | $resources = []; |
|
215 | ||
216 | if (is_array($value)) { |
|
217 | if (Arr::isAssoc($value)) { |
|
218 | $resources[] = array_get($value, $idField); |
|
219 | } else { |
|
220 | $resources = array_column($value, $idField); |
|
221 | } |
|
222 | } else { |
|
223 | $resources[] = $value; |
|
224 | } |
|
225 | ||
226 | return $model::find($resources)->pluck($textField, $idField)->toArray(); |
|
227 | }; |
|
228 | ||
229 | return $this; |
|
230 | } |
|
231 | ||
232 | /** |
|
233 | * Load options from remote. |
@@ 100-129 (lines=30) @@ | ||
97 | * |
|
98 | * @return $this |
|
99 | */ |
|
100 | public function model($model, $idField = 'id', $textField = 'name') |
|
101 | { |
|
102 | if (!class_exists($model) |
|
103 | || !in_array(Model::class, class_parents($model)) |
|
104 | ) { |
|
105 | throw new \InvalidArgumentException("[$model] must be a valid model class"); |
|
106 | } |
|
107 | ||
108 | $this->options = function ($value) use ($model, $idField, $textField) { |
|
109 | if (empty($value)) { |
|
110 | return []; |
|
111 | } |
|
112 | ||
113 | $resources = []; |
|
114 | ||
115 | if (is_array($value)) { |
|
116 | if (Arr::isAssoc($value)) { |
|
117 | $resources[] = array_get($value, $idField); |
|
118 | } else { |
|
119 | $resources = array_column($value, $idField); |
|
120 | } |
|
121 | } else { |
|
122 | $resources[] = $value; |
|
123 | } |
|
124 | ||
125 | return $model::find($resources)->pluck($textField, $idField)->toArray(); |
|
126 | }; |
|
127 | ||
128 | return $this; |
|
129 | } |
|
130 | ||
131 | /** |
|
132 | * Load options from remote. |