@@ 111-140 (lines=30) @@ | ||
108 | * |
|
109 | * @return $this |
|
110 | */ |
|
111 | public function model($model, $idField = 'id', $textField = 'name') |
|
112 | { |
|
113 | if (!class_exists($model) |
|
114 | || !in_array(Model::class, class_parents($model)) |
|
115 | ) { |
|
116 | throw new \InvalidArgumentException("[$model] must be a valid model class"); |
|
117 | } |
|
118 | ||
119 | $this->options = function ($value) use ($model, $idField, $textField) { |
|
120 | if (empty($value)) { |
|
121 | return []; |
|
122 | } |
|
123 | ||
124 | $resources = []; |
|
125 | ||
126 | if (is_array($value)) { |
|
127 | if (Arr::isAssoc($value)) { |
|
128 | $resources[] = array_get($value, $idField); |
|
129 | } else { |
|
130 | $resources = array_column($value, $idField); |
|
131 | } |
|
132 | } else { |
|
133 | $resources[] = $value; |
|
134 | } |
|
135 | ||
136 | return $model::find($resources)->pluck($textField, $idField)->toArray(); |
|
137 | }; |
|
138 | ||
139 | return $this; |
|
140 | } |
|
141 | ||
142 | /** |
|
143 | * Load options from remote. |
@@ 295-324 (lines=30) @@ | ||
292 | * |
|
293 | * @return $this |
|
294 | */ |
|
295 | public function model($model, $idField = 'id', $textField = 'name') |
|
296 | { |
|
297 | if ( |
|
298 | !class_exists($model) |
|
299 | || !in_array(Model::class, class_parents($model)) |
|
300 | ) { |
|
301 | throw new \InvalidArgumentException("[$model] must be a valid model class"); |
|
302 | } |
|
303 | ||
304 | $this->options = function ($value) use ($model, $idField, $textField) { |
|
305 | if (empty($value)) { |
|
306 | return []; |
|
307 | } |
|
308 | ||
309 | $resources = []; |
|
310 | ||
311 | if (is_array($value)) { |
|
312 | if (Arr::isAssoc($value)) { |
|
313 | $resources[] = array_get($value, $idField); |
|
314 | } else { |
|
315 | $resources = array_column($value, $idField); |
|
316 | } |
|
317 | } else { |
|
318 | $resources[] = $value; |
|
319 | } |
|
320 | ||
321 | return $model::find($resources)->pluck($textField, $idField)->toArray(); |
|
322 | }; |
|
323 | ||
324 | return $this; |
|
325 | } |
|
326 | ||
327 | /** |