Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Form often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Form, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Form implements Renderable |
||
| 33 | { |
||
| 34 | use HasHooks, |
||
| 35 | HasFields, |
||
| 36 | ShouldSnakeAttributes; |
||
| 37 | /** |
||
| 38 | * Remove flag in `has many` form. |
||
| 39 | */ |
||
| 40 | const REMOVE_FLAG_NAME = '_remove_'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Eloquent model of the form. |
||
| 44 | * |
||
| 45 | * @var Model |
||
| 46 | */ |
||
| 47 | protected $model; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var \Illuminate\Validation\Validator |
||
| 51 | */ |
||
| 52 | protected $validator; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Builder |
||
| 56 | */ |
||
| 57 | protected $builder; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Data for save to current model from input. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $updates = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Data for save to model's relations from input. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $relations = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Input data. |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $inputs = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var Layout |
||
| 82 | */ |
||
| 83 | protected $layout; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Ignored saving fields. |
||
| 87 | * |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | protected $ignored = []; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Collected field assets. |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | protected static $collectedAssets = []; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var Form\Tab |
||
| 101 | */ |
||
| 102 | protected $tab = null; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Field rows in form. |
||
| 106 | * |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | public $rows = []; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var bool |
||
| 113 | */ |
||
| 114 | protected $isSoftDeletes = false; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Create a new form instance. |
||
| 118 | * |
||
| 119 | * @param $model |
||
| 120 | * @param \Closure $callback |
||
| 121 | */ |
||
| 122 | public function __construct($model, Closure $callback = null) |
||
| 123 | { |
||
| 124 | $this->model = $model; |
||
| 125 | |||
| 126 | $this->builder = new Builder($this); |
||
| 127 | |||
| 128 | $this->initLayout(); |
||
| 129 | |||
| 130 | if ($callback instanceof Closure) { |
||
| 131 | $callback($this); |
||
| 132 | } |
||
| 133 | |||
| 134 | $this->isSoftDeletes = in_array(SoftDeletes::class, class_uses_deep($this->model), true); |
||
| 135 | |||
| 136 | $this->callInitCallbacks(); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param Field $field |
||
| 141 | * |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | public function pushField(Field $field): self |
||
| 145 | { |
||
| 146 | $field->setForm($this); |
||
| 147 | |||
| 148 | $width = $this->builder->getWidth(); |
||
| 149 | $field->setWidth($width['field'], $width['label']); |
||
| 150 | |||
| 151 | $this->builder->fields()->push($field); |
||
| 152 | $this->layout->addField($field); |
||
| 153 | |||
| 154 | return $this; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return Model |
||
| 159 | */ |
||
| 160 | public function model(): Model |
||
| 161 | { |
||
| 162 | return $this->model; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return Builder |
||
| 167 | */ |
||
| 168 | public function builder(): Builder |
||
| 169 | { |
||
| 170 | return $this->builder; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Generate a edit form. |
||
| 175 | * |
||
| 176 | * @param $id |
||
| 177 | * |
||
| 178 | * @return $this |
||
| 179 | */ |
||
| 180 | public function edit($id): self |
||
| 181 | { |
||
| 182 | $this->builder->setMode(Builder::MODE_EDIT); |
||
| 183 | $this->builder->setResourceId($id); |
||
| 184 | |||
| 185 | $this->setFieldValue($id); |
||
| 186 | |||
| 187 | return $this; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Use tab to split form. |
||
| 192 | * |
||
| 193 | * @param string $title |
||
| 194 | * @param Closure $content |
||
| 195 | * @param bool $active |
||
| 196 | * |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | public function tab($title, Closure $content, bool $active = false): self |
||
| 200 | { |
||
| 201 | $this->setTab()->append($title, $content, $active); |
||
| 202 | |||
| 203 | return $this; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get Tab instance. |
||
| 208 | * |
||
| 209 | * @return Tab |
||
| 210 | */ |
||
| 211 | public function getTab() |
||
| 212 | { |
||
| 213 | return $this->tab; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Set Tab instance. |
||
| 218 | * |
||
| 219 | * @return Tab |
||
| 220 | */ |
||
| 221 | public function setTab(): Tab |
||
| 222 | { |
||
| 223 | if ($this->tab === null) { |
||
| 224 | $this->tab = new Tab($this); |
||
| 225 | } |
||
| 226 | |||
| 227 | return $this->tab; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Destroy data entity and remove files. |
||
| 232 | * |
||
| 233 | * @param $id |
||
| 234 | * |
||
| 235 | * @return mixed |
||
| 236 | */ |
||
| 237 | public function destroy($id) |
||
| 238 | { |
||
| 239 | try { |
||
| 240 | if (($ret = $this->callDeleting($id)) instanceof Response) { |
||
| 241 | return $ret; |
||
| 242 | } |
||
| 243 | |||
| 244 | collect(explode(',', $id))->filter()->each(function ($id) { |
||
| 245 | $builder = $this->model()->newQuery(); |
||
| 246 | |||
| 247 | if ($this->isSoftDeletes) { |
||
| 248 | $builder = $builder->withTrashed(); |
||
| 249 | } |
||
| 250 | |||
| 251 | $model = $builder->with($this->getRelations())->findOrFail($id); |
||
| 252 | |||
| 253 | if ($this->isSoftDeletes && $model->trashed()) { |
||
| 254 | $this->deleteFiles($model, true); |
||
| 255 | $model->forceDelete(); |
||
| 256 | |||
| 257 | return; |
||
| 258 | } |
||
| 259 | |||
| 260 | $this->deleteFiles($model); |
||
| 261 | $model->delete(); |
||
| 262 | }); |
||
| 263 | |||
| 264 | if (($ret = $this->callDeleted()) instanceof Response) { |
||
| 265 | return $ret; |
||
| 266 | } |
||
| 267 | |||
| 268 | $response = [ |
||
| 269 | 'status' => true, |
||
| 270 | 'message' => trans('admin.delete_succeeded'), |
||
| 271 | ]; |
||
| 272 | } catch (\Exception $exception) { |
||
| 273 | $response = [ |
||
| 274 | 'status' => false, |
||
| 275 | 'message' => $exception->getMessage() ?: trans('admin.delete_failed'), |
||
| 276 | ]; |
||
| 277 | } |
||
| 278 | |||
| 279 | return response()->json($response); |
||
|
|
|||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Remove files in record. |
||
| 284 | * |
||
| 285 | * @param Model $model |
||
| 286 | * @param bool $forceDelete |
||
| 287 | */ |
||
| 288 | protected function deleteFiles(Model $model, $forceDelete = false) |
||
| 289 | { |
||
| 290 | // If it's a soft delete, the files in the data will not be deleted. |
||
| 291 | if (!$forceDelete && $this->isSoftDeletes) { |
||
| 292 | return; |
||
| 293 | } |
||
| 294 | |||
| 295 | $data = $model->toArray(); |
||
| 296 | |||
| 297 | $this->builder->fields()->filter(function ($field) { |
||
| 298 | return $field instanceof Field\File; |
||
| 299 | })->each(function (Field\File $file) use ($data) { |
||
| 300 | $file->setOriginal($data); |
||
| 301 | |||
| 302 | $file->destroy(); |
||
| 303 | }); |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Store a new record. |
||
| 308 | * |
||
| 309 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse |
||
| 310 | */ |
||
| 311 | public function store() |
||
| 312 | { |
||
| 313 | $data = \request()->all(); |
||
| 314 | |||
| 315 | // Handle validation errors. |
||
| 316 | if ($validationMessages = $this->validationMessages($data)) { |
||
| 317 | return $this->responseValidationError($validationMessages); |
||
| 318 | } |
||
| 319 | |||
| 320 | if (($response = $this->prepare($data)) instanceof Response) { |
||
| 321 | return $response; |
||
| 322 | } |
||
| 323 | |||
| 324 | View Code Duplication | DB::transaction(function () { |
|
| 325 | $inserts = $this->prepareInsert($this->updates); |
||
| 326 | |||
| 327 | foreach ($inserts as $column => $value) { |
||
| 328 | $this->model->setAttribute($column, $value); |
||
| 329 | } |
||
| 330 | |||
| 331 | $this->model->save(); |
||
| 332 | |||
| 333 | $this->updateRelation($this->relations); |
||
| 334 | }); |
||
| 335 | |||
| 336 | if (($response = $this->callSaved()) instanceof Response) { |
||
| 337 | return $response; |
||
| 338 | } |
||
| 339 | |||
| 340 | if ($response = $this->ajaxResponse(trans('admin.save_succeeded'))) { |
||
| 341 | return $response; |
||
| 342 | } |
||
| 343 | |||
| 344 | return $this->redirectAfterStore(); |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param MessageBag $message |
||
| 349 | * |
||
| 350 | * @return $this|\Illuminate\Http\JsonResponse |
||
| 351 | */ |
||
| 352 | protected function responseValidationError(MessageBag $message) |
||
| 353 | { |
||
| 354 | if (\request()->ajax() && !\request()->pjax()) { |
||
| 355 | return response()->json([ |
||
| 356 | 'status' => false, |
||
| 357 | 'validation' => $message, |
||
| 358 | 'message' => $message->first(), |
||
| 359 | ]); |
||
| 360 | } |
||
| 361 | |||
| 362 | return back()->withInput()->withErrors($message); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Get ajax response. |
||
| 367 | * |
||
| 368 | * @param string $message |
||
| 369 | * |
||
| 370 | * @return bool|\Illuminate\Http\JsonResponse |
||
| 371 | */ |
||
| 372 | protected function ajaxResponse($message) |
||
| 373 | { |
||
| 374 | $request = Request::capture(); |
||
| 375 | |||
| 376 | // ajax but not pjax |
||
| 377 | if ($request->ajax() && !$request->pjax()) { |
||
| 378 | return response()->json([ |
||
| 379 | 'status' => true, |
||
| 380 | 'message' => $message, |
||
| 381 | 'display' => $this->applayFieldDisplay() |
||
| 382 | ]); |
||
| 383 | } |
||
| 384 | |||
| 385 | return false; |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @return array |
||
| 390 | */ |
||
| 391 | protected function applayFieldDisplay() |
||
| 392 | { |
||
| 393 | $editable = []; |
||
| 394 | |||
| 395 | /** @var Field $field */ |
||
| 396 | foreach ($this->builder()->fields() as $field) { |
||
| 397 | if (! \request()->has($field->column())) { |
||
| 398 | continue; |
||
| 399 | } |
||
| 400 | |||
| 401 | $newValue = $this->model->fresh()->getAttribute($field->column()); |
||
| 402 | |||
| 403 | if ($newValue instanceof Arrayable) { |
||
| 404 | $newValue = $newValue->toArray(); |
||
| 405 | } |
||
| 406 | |||
| 407 | if ($field instanceof Field\BelongsTo || $field instanceof Field\BelongsToMany) { |
||
| 408 | $selectable = $field->getSelectable(); |
||
| 409 | |||
| 410 | if (method_exists($selectable, 'display')) { |
||
| 411 | $display = $selectable::display(); |
||
| 412 | |||
| 413 | $editable[$field->column()] = $display->call($this->model, $newValue); |
||
| 414 | } |
||
| 415 | } |
||
| 416 | } |
||
| 417 | |||
| 418 | return $editable; |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Prepare input data for insert or update. |
||
| 423 | * |
||
| 424 | * @param array $data |
||
| 425 | * |
||
| 426 | * @return mixed |
||
| 427 | */ |
||
| 428 | protected function prepare($data = []) |
||
| 429 | { |
||
| 430 | if (($response = $this->callSubmitted()) instanceof Response) { |
||
| 431 | return $response; |
||
| 432 | } |
||
| 433 | |||
| 434 | $this->inputs = array_merge($this->removeIgnoredFields($data), $this->inputs); |
||
| 435 | |||
| 436 | if (($response = $this->callSaving()) instanceof Response) { |
||
| 437 | return $response; |
||
| 438 | } |
||
| 439 | |||
| 440 | $this->relations = $this->getRelationInputs($this->inputs); |
||
| 441 | |||
| 442 | $this->updates = Arr::except($this->inputs, array_keys($this->relations)); |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Remove ignored fields from input. |
||
| 447 | * |
||
| 448 | * @param array $input |
||
| 449 | * |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | protected function removeIgnoredFields($input): array |
||
| 453 | { |
||
| 454 | Arr::forget($input, $this->ignored); |
||
| 455 | |||
| 456 | return $input; |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Get inputs for relations. |
||
| 461 | * |
||
| 462 | * @param array $inputs |
||
| 463 | * |
||
| 464 | * @return array |
||
| 465 | */ |
||
| 466 | protected function getRelationInputs($inputs = []): array |
||
| 467 | { |
||
| 468 | $relations = []; |
||
| 469 | |||
| 470 | foreach ($inputs as $column => $value) { |
||
| 471 | if (method_exists($this->model, $column) || |
||
| 472 | method_exists($this->model, $column = Str::camel($column))) { |
||
| 473 | $relation = call_user_func([$this->model, $column]); |
||
| 474 | |||
| 475 | if ($relation instanceof Relations\Relation) { |
||
| 476 | $relations[$column] = $value; |
||
| 477 | } |
||
| 478 | } |
||
| 479 | } |
||
| 480 | |||
| 481 | return $relations; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Handle update. |
||
| 486 | * |
||
| 487 | * @param int $id |
||
| 488 | * @param null $data |
||
| 489 | * |
||
| 490 | * @return bool|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|mixed|null|Response |
||
| 491 | */ |
||
| 492 | public function update($id, $data = null) |
||
| 493 | { |
||
| 494 | $data = ($data) ?: request()->all(); |
||
| 495 | |||
| 496 | $isEditable = $this->isEditable($data); |
||
| 497 | |||
| 498 | if (($data = $this->handleColumnUpdates($id, $data)) instanceof Response) { |
||
| 499 | return $data; |
||
| 500 | } |
||
| 501 | |||
| 502 | /* @var Model $this ->model */ |
||
| 503 | $builder = $this->model(); |
||
| 504 | |||
| 505 | if ($this->isSoftDeletes) { |
||
| 506 | $builder = $builder->withTrashed(); |
||
| 507 | } |
||
| 508 | |||
| 509 | $this->model = $builder->with($this->getRelations())->findOrFail($id); |
||
| 510 | |||
| 511 | $this->setFieldOriginalValue(); |
||
| 512 | |||
| 513 | // Handle validation errors. |
||
| 514 | if ($validationMessages = $this->validationMessages($data)) { |
||
| 515 | if (!$isEditable) { |
||
| 516 | return back()->withInput()->withErrors($validationMessages); |
||
| 517 | } |
||
| 518 | |||
| 519 | return response()->json(['errors' => Arr::dot($validationMessages->getMessages())], 422); |
||
| 520 | } |
||
| 521 | |||
| 522 | if (($response = $this->prepare($data)) instanceof Response) { |
||
| 523 | return $response; |
||
| 524 | } |
||
| 525 | |||
| 526 | View Code Duplication | DB::transaction(function () { |
|
| 527 | $updates = $this->prepareUpdate($this->updates); |
||
| 528 | |||
| 529 | foreach ($updates as $column => $value) { |
||
| 530 | /* @var Model $this ->model */ |
||
| 531 | $this->model->setAttribute($column, $value); |
||
| 532 | } |
||
| 533 | |||
| 534 | $this->model->save(); |
||
| 535 | |||
| 536 | $this->updateRelation($this->relations); |
||
| 537 | }); |
||
| 538 | |||
| 539 | if (($result = $this->callSaved()) instanceof Response) { |
||
| 540 | return $result; |
||
| 541 | } |
||
| 542 | |||
| 543 | if ($response = $this->ajaxResponse(trans('admin.update_succeeded'))) { |
||
| 544 | return $response; |
||
| 545 | } |
||
| 546 | |||
| 547 | return $this->redirectAfterUpdate($id); |
||
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Get RedirectResponse after store. |
||
| 552 | * |
||
| 553 | * @return \Illuminate\Http\RedirectResponse |
||
| 554 | */ |
||
| 555 | protected function redirectAfterStore() |
||
| 556 | { |
||
| 557 | $resourcesPath = $this->resource(0); |
||
| 558 | |||
| 559 | $key = $this->model->getKey(); |
||
| 560 | |||
| 561 | return $this->redirectAfterSaving($resourcesPath, $key); |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Get RedirectResponse after update. |
||
| 566 | * |
||
| 567 | * @param mixed $key |
||
| 568 | * |
||
| 569 | * @return \Illuminate\Http\RedirectResponse |
||
| 570 | */ |
||
| 571 | protected function redirectAfterUpdate($key) |
||
| 572 | { |
||
| 573 | $resourcesPath = $this->resource(-1); |
||
| 574 | |||
| 575 | return $this->redirectAfterSaving($resourcesPath, $key); |
||
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Get RedirectResponse after data saving. |
||
| 580 | * |
||
| 581 | * @param string $resourcesPath |
||
| 582 | * @param string $key |
||
| 583 | * |
||
| 584 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
| 585 | */ |
||
| 586 | protected function redirectAfterSaving($resourcesPath, $key) |
||
| 587 | { |
||
| 588 | if (request('after-save') == 1) { |
||
| 589 | // continue editing |
||
| 590 | $url = rtrim($resourcesPath, '/')."/{$key}/edit"; |
||
| 591 | } elseif (request('after-save') == 2) { |
||
| 592 | // continue creating |
||
| 593 | $url = rtrim($resourcesPath, '/').'/create'; |
||
| 594 | } elseif (request('after-save') == 3) { |
||
| 595 | // view resource |
||
| 596 | $url = rtrim($resourcesPath, '/')."/{$key}"; |
||
| 597 | } else { |
||
| 598 | $url = request(Builder::PREVIOUS_URL_KEY) ?: $resourcesPath; |
||
| 599 | } |
||
| 600 | |||
| 601 | admin_toastr(trans('admin.save_succeeded')); |
||
| 602 | |||
| 603 | return redirect($url); |
||
| 604 | } |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Check if request is from editable. |
||
| 608 | * |
||
| 609 | * @param array $input |
||
| 610 | * |
||
| 611 | * @return bool |
||
| 612 | */ |
||
| 613 | protected function isEditable(array $input = []): bool |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Handle updates for single column. |
||
| 620 | * |
||
| 621 | * @param int $id |
||
| 622 | * @param array $data |
||
| 623 | * |
||
| 624 | * @return array|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|Response |
||
| 625 | */ |
||
| 626 | protected function handleColumnUpdates($id, $data) |
||
| 627 | { |
||
| 628 | $data = $this->handleEditable($data); |
||
| 629 | |||
| 630 | $data = $this->handleFileDelete($data); |
||
| 631 | |||
| 632 | $data = $this->handleFileSort($data); |
||
| 633 | |||
| 634 | if ($this->handleOrderable($id, $data)) { |
||
| 635 | return response([ |
||
| 636 | 'status' => true, |
||
| 637 | 'message' => trans('admin.update_succeeded'), |
||
| 638 | ]); |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Handle editable update. |
||
| 646 | * |
||
| 647 | * @param array $input |
||
| 648 | * |
||
| 649 | * @return array |
||
| 650 | */ |
||
| 651 | protected function handleEditable(array $input = []): array |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @param array $input |
||
| 666 | * |
||
| 667 | * @return array |
||
| 668 | */ |
||
| 669 | protected function handleFileDelete(array $input = []): array |
||
| 680 | |||
| 681 | /** |
||
| 682 | * @param array $input |
||
| 683 | * |
||
| 684 | * @return array |
||
| 685 | */ |
||
| 686 | protected function handleFileSort(array $input = []): array |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Handle orderable update. |
||
| 709 | * |
||
| 710 | * @param int $id |
||
| 711 | * @param array $input |
||
| 712 | * |
||
| 713 | * @return bool |
||
| 714 | */ |
||
| 715 | protected function handleOrderable($id, array $input = []) |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Update relation data. |
||
| 732 | * |
||
| 733 | * @param array $relationsData |
||
| 734 | * |
||
| 735 | * @return void |
||
| 736 | */ |
||
| 737 | protected function updateRelation($relationsData) |
||
| 844 | |||
| 845 | /** |
||
| 846 | * Prepare input data for update. |
||
| 847 | * |
||
| 848 | * @param array $updates |
||
| 849 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
| 850 | * |
||
| 851 | * @return array |
||
| 852 | */ |
||
| 853 | protected function prepareUpdate(array $updates, $oneToOneRelation = false): array |
||
| 885 | |||
| 886 | /** |
||
| 887 | * @param string|array $columns |
||
| 888 | * @param bool $containsDot |
||
| 889 | * |
||
| 890 | * @return bool |
||
| 891 | */ |
||
| 892 | protected function isInvalidColumn($columns, $containsDot = false): bool |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Prepare input data for insert. |
||
| 906 | * |
||
| 907 | * @param $inserts |
||
| 908 | * |
||
| 909 | * @return array |
||
| 910 | */ |
||
| 911 | protected function prepareInsert($inserts): array |
||
| 934 | |||
| 935 | /** |
||
| 936 | * Is input data is has-one relation. |
||
| 937 | * |
||
| 938 | * @param array $inserts |
||
| 939 | * |
||
| 940 | * @return bool |
||
| 941 | */ |
||
| 942 | protected function isHasOneRelation($inserts): bool |
||
| 956 | |||
| 957 | /** |
||
| 958 | * Ignore fields to save. |
||
| 959 | * |
||
| 960 | * @param string|array $fields |
||
| 961 | * |
||
| 962 | * @return $this |
||
| 963 | */ |
||
| 964 | public function ignore($fields): self |
||
| 970 | |||
| 971 | /** |
||
| 972 | * @param array $data |
||
| 973 | * @param string|array $columns |
||
| 974 | * |
||
| 975 | * @return array|mixed |
||
| 976 | */ |
||
| 977 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
| 995 | |||
| 996 | /** |
||
| 997 | * Find field object by column. |
||
| 998 | * |
||
| 999 | * @param $column |
||
| 1000 | * |
||
| 1001 | * @return mixed |
||
| 1002 | */ |
||
| 1003 | protected function getFieldByColumn($column) |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Set original data for each field. |
||
| 1018 | * |
||
| 1019 | * @return void |
||
| 1020 | */ |
||
| 1021 | protected function setFieldOriginalValue() |
||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Set all fields value in form. |
||
| 1032 | * |
||
| 1033 | * @param $id |
||
| 1034 | * |
||
| 1035 | * @return void |
||
| 1036 | */ |
||
| 1037 | protected function setFieldValue($id) |
||
| 1059 | |||
| 1060 | /** |
||
| 1061 | * Add a fieldset to form. |
||
| 1062 | * |
||
| 1063 | * @param string $title |
||
| 1064 | * @param Closure $setCallback |
||
| 1065 | * |
||
| 1066 | * @return Field\Fieldset |
||
| 1067 | */ |
||
| 1068 | View Code Duplication | public function fieldset(string $title, Closure $setCallback) |
|
| 1080 | |||
| 1081 | /** |
||
| 1082 | * Get validation messages. |
||
| 1083 | * |
||
| 1084 | * @param array $input |
||
| 1085 | * |
||
| 1086 | * @return MessageBag|bool |
||
| 1087 | */ |
||
| 1088 | public function validationMessages($input) |
||
| 1107 | |||
| 1108 | /** |
||
| 1109 | * Merge validation messages from input validators. |
||
| 1110 | * |
||
| 1111 | * @param \Illuminate\Validation\Validator[] $validators |
||
| 1112 | * |
||
| 1113 | * @return MessageBag |
||
| 1114 | */ |
||
| 1115 | protected function mergeValidationMessages($validators): MessageBag |
||
| 1125 | |||
| 1126 | /** |
||
| 1127 | * Get all relations of model from callable. |
||
| 1128 | * |
||
| 1129 | * @return array |
||
| 1130 | */ |
||
| 1131 | public function getRelations(): array |
||
| 1158 | |||
| 1159 | /** |
||
| 1160 | * Set action for form. |
||
| 1161 | * |
||
| 1162 | * @param string $action |
||
| 1163 | * |
||
| 1164 | * @return $this |
||
| 1165 | */ |
||
| 1166 | public function setAction($action): self |
||
| 1172 | |||
| 1173 | /** |
||
| 1174 | * Set field and label width in current form. |
||
| 1175 | * |
||
| 1176 | * @param int $fieldWidth |
||
| 1177 | * @param int $labelWidth |
||
| 1178 | * |
||
| 1179 | * @return $this |
||
| 1180 | */ |
||
| 1181 | public function setWidth($fieldWidth = 8, $labelWidth = 2): self |
||
| 1192 | |||
| 1193 | /** |
||
| 1194 | * Set view for form. |
||
| 1195 | * |
||
| 1196 | * @param string $view |
||
| 1197 | * |
||
| 1198 | * @return $this |
||
| 1199 | */ |
||
| 1200 | public function setView($view): self |
||
| 1206 | |||
| 1207 | /** |
||
| 1208 | * Set title for form. |
||
| 1209 | * |
||
| 1210 | * @param string $title |
||
| 1211 | * |
||
| 1212 | * @return $this |
||
| 1213 | */ |
||
| 1214 | public function setTitle($title = ''): self |
||
| 1220 | |||
| 1221 | /** |
||
| 1222 | * Add a row in form. |
||
| 1223 | * |
||
| 1224 | * @param Closure $callback |
||
| 1225 | * |
||
| 1226 | * @return $this |
||
| 1227 | */ |
||
| 1228 | public function row(Closure $callback): self |
||
| 1234 | |||
| 1235 | /** |
||
| 1236 | * Tools setting for form. |
||
| 1237 | * |
||
| 1238 | * @param Closure $callback |
||
| 1239 | */ |
||
| 1240 | public function tools(Closure $callback) |
||
| 1244 | |||
| 1245 | /** |
||
| 1246 | * @param Closure|null $callback |
||
| 1247 | * |
||
| 1248 | * @return Form\Tools |
||
| 1249 | */ |
||
| 1250 | View Code Duplication | public function header(Closure $callback = null) |
|
| 1258 | |||
| 1259 | /** |
||
| 1260 | * Indicates if current form page is creating. |
||
| 1261 | * |
||
| 1262 | * @return bool |
||
| 1263 | */ |
||
| 1264 | public function isCreating(): bool |
||
| 1268 | |||
| 1269 | /** |
||
| 1270 | * Indicates if current form page is editing. |
||
| 1271 | * |
||
| 1272 | * @return bool |
||
| 1273 | */ |
||
| 1274 | public function isEditing(): bool |
||
| 1278 | |||
| 1279 | /** |
||
| 1280 | * Disable form submit. |
||
| 1281 | * |
||
| 1282 | * @param bool $disable |
||
| 1283 | * |
||
| 1284 | * @return $this |
||
| 1285 | * |
||
| 1286 | * @deprecated |
||
| 1287 | */ |
||
| 1288 | public function disableSubmit(bool $disable = true): self |
||
| 1294 | |||
| 1295 | /** |
||
| 1296 | * Disable form reset. |
||
| 1297 | * |
||
| 1298 | * @param bool $disable |
||
| 1299 | * |
||
| 1300 | * @return $this |
||
| 1301 | * |
||
| 1302 | * @deprecated |
||
| 1303 | */ |
||
| 1304 | public function disableReset(bool $disable = true): self |
||
| 1310 | |||
| 1311 | /** |
||
| 1312 | * Disable View Checkbox on footer. |
||
| 1313 | * |
||
| 1314 | * @param bool $disable |
||
| 1315 | * |
||
| 1316 | * @return $this |
||
| 1317 | */ |
||
| 1318 | public function disableViewCheck(bool $disable = true): self |
||
| 1324 | |||
| 1325 | /** |
||
| 1326 | * Disable Editing Checkbox on footer. |
||
| 1327 | * |
||
| 1328 | * @param bool $disable |
||
| 1329 | * |
||
| 1330 | * @return $this |
||
| 1331 | */ |
||
| 1332 | public function disableEditingCheck(bool $disable = true): self |
||
| 1338 | |||
| 1339 | /** |
||
| 1340 | * Disable Creating Checkbox on footer. |
||
| 1341 | * |
||
| 1342 | * @param bool $disable |
||
| 1343 | * |
||
| 1344 | * @return $this |
||
| 1345 | */ |
||
| 1346 | public function disableCreatingCheck(bool $disable = true): self |
||
| 1352 | |||
| 1353 | /** |
||
| 1354 | * Footer setting for form. |
||
| 1355 | * |
||
| 1356 | * @param Closure $callback |
||
| 1357 | * |
||
| 1358 | * @return \Encore\Admin\Form\Footer |
||
| 1359 | */ |
||
| 1360 | View Code Duplication | public function footer(Closure $callback = null) |
|
| 1368 | |||
| 1369 | /** |
||
| 1370 | * Get current resource route url. |
||
| 1371 | * |
||
| 1372 | * @param int $slice |
||
| 1373 | * |
||
| 1374 | * @return string |
||
| 1375 | */ |
||
| 1376 | public function resource($slice = -2): string |
||
| 1386 | |||
| 1387 | /** |
||
| 1388 | * Render the form contents. |
||
| 1389 | * |
||
| 1390 | * @return string |
||
| 1391 | */ |
||
| 1392 | public function render() |
||
| 1400 | |||
| 1401 | /** |
||
| 1402 | * Get or set input data. |
||
| 1403 | * |
||
| 1404 | * @param string $key |
||
| 1405 | * @param null $value |
||
| 1406 | * |
||
| 1407 | * @return array|mixed |
||
| 1408 | */ |
||
| 1409 | public function input($key, $value = null) |
||
| 1417 | |||
| 1418 | /** |
||
| 1419 | * Add a new layout column. |
||
| 1420 | * |
||
| 1421 | * @param int $width |
||
| 1422 | * @param \Closure $closure |
||
| 1423 | * |
||
| 1424 | * @return $this |
||
| 1425 | */ |
||
| 1426 | View Code Duplication | public function column($width, \Closure $closure): self |
|
| 1434 | |||
| 1435 | /** |
||
| 1436 | * Initialize filter layout. |
||
| 1437 | */ |
||
| 1438 | protected function initLayout() |
||
| 1442 | |||
| 1443 | /** |
||
| 1444 | * Getter. |
||
| 1445 | * |
||
| 1446 | * @param string $name |
||
| 1447 | * |
||
| 1448 | * @return array|mixed |
||
| 1449 | */ |
||
| 1450 | public function __get($name) |
||
| 1454 | |||
| 1455 | /** |
||
| 1456 | * Setter. |
||
| 1457 | * |
||
| 1458 | * @param string $name |
||
| 1459 | * @param mixed $value |
||
| 1460 | * |
||
| 1461 | * @return array |
||
| 1462 | */ |
||
| 1463 | public function __set($name, $value) |
||
| 1467 | |||
| 1468 | /** |
||
| 1469 | * Generate a Field object and add to form builder if Field exists. |
||
| 1470 | * |
||
| 1471 | * @param string $method |
||
| 1472 | * @param array $arguments |
||
| 1473 | * |
||
| 1474 | * @return Field |
||
| 1475 | */ |
||
| 1476 | public function __call($method, $arguments) |
||
| 1492 | |||
| 1493 | /** |
||
| 1494 | * @return Layout |
||
| 1495 | */ |
||
| 1496 | public function getLayout(): Layout |
||
| 1500 | } |
||
| 1501 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: