| @@ 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. |
|
| @@ 257-286 (lines=30) @@ | ||
| 254 | * |
|
| 255 | * @return $this |
|
| 256 | */ |
|
| 257 | public function model($model, $idField = 'id', $textField = 'name') |
|
| 258 | { |
|
| 259 | if ( |
|
| 260 | !class_exists($model) |
|
| 261 | || !in_array(Model::class, class_parents($model)) |
|
| 262 | ) { |
|
| 263 | throw new \InvalidArgumentException("[$model] must be a valid model class"); |
|
| 264 | } |
|
| 265 | ||
| 266 | $this->options = function ($value) use ($model, $idField, $textField) { |
|
| 267 | if (empty($value)) { |
|
| 268 | return []; |
|
| 269 | } |
|
| 270 | ||
| 271 | $resources = []; |
|
| 272 | ||
| 273 | if (is_array($value)) { |
|
| 274 | if (Arr::isAssoc($value)) { |
|
| 275 | $resources[] = array_get($value, $idField); |
|
| 276 | } else { |
|
| 277 | $resources = array_column($value, $idField); |
|
| 278 | } |
|
| 279 | } else { |
|
| 280 | $resources[] = $value; |
|
| 281 | } |
|
| 282 | ||
| 283 | return $model::find($resources)->pluck($textField, $idField)->toArray(); |
|
| 284 | }; |
|
| 285 | ||
| 286 | return $this; |
|
| 287 | } |
|
| 288 | ||
| 289 | /** |
|