| @@ 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. |
|
| @@ 296-325 (lines=30) @@ | ||
| 293 | * |
|
| 294 | * @return $this |
|
| 295 | */ |
|
| 296 | public function model($model, $idField = 'id', $textField = 'name') |
|
| 297 | { |
|
| 298 | if ( |
|
| 299 | !class_exists($model) |
|
| 300 | || !in_array(Model::class, class_parents($model)) |
|
| 301 | ) { |
|
| 302 | throw new \InvalidArgumentException("[$model] must be a valid model class"); |
|
| 303 | } |
|
| 304 | ||
| 305 | $this->options = function ($value) use ($model, $idField, $textField) { |
|
| 306 | if (empty($value)) { |
|
| 307 | return []; |
|
| 308 | } |
|
| 309 | ||
| 310 | $resources = []; |
|
| 311 | ||
| 312 | if (is_array($value)) { |
|
| 313 | if (Arr::isAssoc($value)) { |
|
| 314 | $resources[] = array_get($value, $idField); |
|
| 315 | } else { |
|
| 316 | $resources = array_column($value, $idField); |
|
| 317 | } |
|
| 318 | } else { |
|
| 319 | $resources[] = $value; |
|
| 320 | } |
|
| 321 | ||
| 322 | return $model::find($resources)->pluck($textField, $idField)->toArray(); |
|
| 323 | }; |
|
| 324 | ||
| 325 | return $this; |
|
| 326 | } |
|
| 327 | ||
| 328 | /** |
|