| @@ 113-142 (lines=30) @@ | ||
| 110 | * |
|
| 111 | * @return $this |
|
| 112 | */ |
|
| 113 | public function model($model, $idField = 'id', $textField = 'name') |
|
| 114 | { |
|
| 115 | if (!class_exists($model) |
|
| 116 | || !in_array(Model::class, class_parents($model)) |
|
| 117 | ) { |
|
| 118 | throw new \InvalidArgumentException("[$model] must be a valid model class"); |
|
| 119 | } |
|
| 120 | ||
| 121 | $this->options = function ($value) use ($model, $idField, $textField) { |
|
| 122 | if (empty($value)) { |
|
| 123 | return []; |
|
| 124 | } |
|
| 125 | ||
| 126 | $resources = []; |
|
| 127 | ||
| 128 | if (is_array($value)) { |
|
| 129 | if (Arr::isAssoc($value)) { |
|
| 130 | $resources[] = Arr::get($value, $idField); |
|
| 131 | } else { |
|
| 132 | $resources = array_column($value, $idField); |
|
| 133 | } |
|
| 134 | } else { |
|
| 135 | $resources[] = $value; |
|
| 136 | } |
|
| 137 | ||
| 138 | return $model::find($resources)->pluck($textField, $idField)->toArray(); |
|
| 139 | }; |
|
| 140 | ||
| 141 | return $this; |
|
| 142 | } |
|
| 143 | ||
| 144 | /** |
|
| 145 | * Load options from remote. |
|
| @@ 226-256 (lines=31) @@ | ||
| 223 | * |
|
| 224 | * @return $this |
|
| 225 | */ |
|
| 226 | public function model($model, $idField = 'id', $textField = 'name') |
|
| 227 | { |
|
| 228 | if ( |
|
| 229 | !class_exists($model) |
|
| 230 | || !in_array(Model::class, class_parents($model)) |
|
| 231 | ) { |
|
| 232 | throw new \InvalidArgumentException("[$model] must be a valid model class"); |
|
| 233 | } |
|
| 234 | $this->options = function ($value) use ($model, $idField, $textField) { |
|
| 235 | if (empty($value)) { |
|
| 236 | return []; |
|
| 237 | } |
|
| 238 | $resources = []; |
|
| 239 | if (is_array($value)) { |
|
| 240 | if (Arr::isAssoc($value)) { |
|
| 241 | $resources[] = Arr::get($value, $idField); |
|
| 242 | } else { |
|
| 243 | $resources = array_column($value, $idField); |
|
| 244 | } |
|
| 245 | } else { |
|
| 246 | $resources[] = $value; |
|
| 247 | } |
|
| 248 | ||
| 249 | return $model::find($resources)->pluck($textField, $idField)->toArray(); |
|
| 250 | }; |
|
| 251 | ||
| 252 | return $this; |
|
| 253 | } |
|
| 254 | ||
| 255 | /** |
|
| 256 | * Load options from remote. |
|
| 257 | * |
|
| 258 | * @param string $url |
|
| 259 | * @param array $parameters |
|