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 |
||
| 73 | class Form implements Renderable |
||
| 74 | { |
||
| 75 | use HasHooks; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Remove flag in `has many` form. |
||
| 79 | */ |
||
| 80 | const REMOVE_FLAG_NAME = '_remove_'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Eloquent model of the form. |
||
| 84 | * |
||
| 85 | * @var Model |
||
| 86 | */ |
||
| 87 | protected $model; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var \Illuminate\Validation\Validator |
||
| 91 | */ |
||
| 92 | protected $validator; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var Builder |
||
| 96 | */ |
||
| 97 | protected $builder; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Data for save to current model from input. |
||
| 101 | * |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | protected $updates = []; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Data for save to model's relations from input. |
||
| 108 | * |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | protected $relations = []; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Input data. |
||
| 115 | * |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | protected $inputs = []; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Available fields. |
||
| 122 | * |
||
| 123 | * @var array |
||
| 124 | */ |
||
| 125 | public static $availableFields = []; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Form field alias. |
||
| 129 | * |
||
| 130 | * @var array |
||
| 131 | */ |
||
| 132 | public static $fieldAlias = []; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Ignored saving fields. |
||
| 136 | * |
||
| 137 | * @var array |
||
| 138 | */ |
||
| 139 | protected $ignored = []; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Collected field assets. |
||
| 143 | * |
||
| 144 | * @var array |
||
| 145 | */ |
||
| 146 | protected static $collectedAssets = []; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var Form\Tab |
||
| 150 | */ |
||
| 151 | protected $tab = null; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Field rows in form. |
||
| 155 | * |
||
| 156 | * @var array |
||
| 157 | */ |
||
| 158 | public $rows = []; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var bool |
||
| 162 | */ |
||
| 163 | protected $isSoftDeletes = false; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Initialization closure array. |
||
| 167 | * |
||
| 168 | * @var []Closure |
||
| 169 | */ |
||
| 170 | protected static $initCallbacks; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Create a new form instance. |
||
| 174 | * |
||
| 175 | * @param $model |
||
| 176 | * @param \Closure $callback |
||
| 177 | */ |
||
| 178 | public function __construct($model, Closure $callback = null) |
||
| 179 | { |
||
| 180 | $this->model = $model; |
||
| 181 | |||
| 182 | $this->builder = new Builder($this); |
||
| 183 | |||
| 184 | if ($callback instanceof Closure) { |
||
| 185 | $callback($this); |
||
| 186 | } |
||
| 187 | |||
| 188 | $this->isSoftDeletes = in_array(SoftDeletes::class, class_uses_deep($this->model)); |
||
| 189 | |||
| 190 | $this->callInitCallbacks(); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Initialize with user pre-defined default disables, etc. |
||
| 195 | * |
||
| 196 | * @param Closure $callback |
||
| 197 | */ |
||
| 198 | public static function init(Closure $callback = null) |
||
| 199 | { |
||
| 200 | static::$initCallbacks[] = $callback; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Call the initialization closure array in sequence. |
||
| 205 | */ |
||
| 206 | protected function callInitCallbacks() |
||
| 207 | { |
||
| 208 | if (empty(static::$initCallbacks)) { |
||
| 209 | return; |
||
| 210 | } |
||
| 211 | |||
| 212 | foreach (static::$initCallbacks as $callback) { |
||
| 213 | call_user_func($callback, $this); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param Field $field |
||
| 219 | * |
||
| 220 | * @return $this |
||
| 221 | */ |
||
| 222 | public function pushField(Field $field) |
||
| 223 | { |
||
| 224 | $field->setForm($this); |
||
| 225 | |||
| 226 | $this->builder->fields()->push($field); |
||
| 227 | |||
| 228 | return $this; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return Model |
||
| 233 | */ |
||
| 234 | public function model() |
||
| 235 | { |
||
| 236 | return $this->model; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return Builder |
||
| 241 | */ |
||
| 242 | public function builder() |
||
| 243 | { |
||
| 244 | return $this->builder; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Generate a edit form. |
||
| 249 | * |
||
| 250 | * @param $id |
||
| 251 | * |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | public function edit($id) |
||
| 255 | { |
||
| 256 | $this->builder->setMode(Builder::MODE_EDIT); |
||
| 257 | $this->builder->setResourceId($id); |
||
| 258 | |||
| 259 | $this->setFieldValue($id); |
||
| 260 | |||
| 261 | return $this; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Use tab to split form. |
||
| 266 | * |
||
| 267 | * @param string $title |
||
| 268 | * @param Closure $content |
||
| 269 | * |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | public function tab($title, Closure $content, $active = false) |
||
| 273 | { |
||
| 274 | $this->getTab()->append($title, $content, $active); |
||
| 275 | |||
| 276 | return $this; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get Tab instance. |
||
| 281 | * |
||
| 282 | * @return Tab |
||
| 283 | */ |
||
| 284 | public function getTab() |
||
| 285 | { |
||
| 286 | if (is_null($this->tab)) { |
||
| 287 | $this->tab = new Tab($this); |
||
| 288 | } |
||
| 289 | |||
| 290 | return $this->tab; |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Destroy data entity and remove files. |
||
| 295 | * |
||
| 296 | * @param $id |
||
| 297 | * |
||
| 298 | * @return mixed |
||
| 299 | */ |
||
| 300 | public function destroy($id) |
||
| 301 | { |
||
| 302 | try { |
||
| 303 | |||
| 304 | if (($ret = $this->callDeleting()) instanceof Response) { |
||
| 305 | return $ret; |
||
| 306 | } |
||
| 307 | |||
| 308 | collect(explode(',', $id))->filter()->each(function ($id) { |
||
| 309 | $builder = $this->model()->newQuery(); |
||
| 310 | |||
| 311 | if ($this->isSoftDeletes) { |
||
| 312 | $builder = $builder->withTrashed(); |
||
| 313 | } |
||
| 314 | |||
| 315 | $model = $builder->with($this->getRelations())->findOrFail($id); |
||
| 316 | |||
| 317 | if ($this->isSoftDeletes && $model->trashed()) { |
||
| 318 | $this->deleteFiles($model, true); |
||
| 319 | $model->forceDelete(); |
||
| 320 | |||
| 321 | return; |
||
| 322 | } |
||
| 323 | |||
| 324 | $this->deleteFiles($model); |
||
| 325 | $model->delete(); |
||
| 326 | }); |
||
| 327 | |||
| 328 | if (($ret = $this->callDeleted()) instanceof Response) { |
||
| 329 | return $ret; |
||
| 330 | } |
||
| 331 | |||
| 332 | $response = [ |
||
| 333 | 'status' => true, |
||
| 334 | 'message' => trans('admin.delete_succeeded'), |
||
| 335 | ]; |
||
| 336 | |||
| 337 | } catch (\Exception $exception) { |
||
| 338 | $response = [ |
||
| 339 | 'status' => false, |
||
| 340 | 'message' => $exception->getMessage() ?: trans('admin.delete_failed'), |
||
| 341 | ]; |
||
| 342 | } |
||
| 343 | |||
| 344 | return response()->json($response); |
||
|
|
|||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Remove files in record. |
||
| 349 | * |
||
| 350 | * @param Model $model |
||
| 351 | * @param bool $forceDelete |
||
| 352 | */ |
||
| 353 | protected function deleteFiles(Model $model, $forceDelete = false) |
||
| 354 | { |
||
| 355 | // If it's a soft delete, the files in the data will not be deleted. |
||
| 356 | if (!$forceDelete && $this->isSoftDeletes) { |
||
| 357 | return; |
||
| 358 | } |
||
| 359 | |||
| 360 | $data = $model->toArray(); |
||
| 361 | |||
| 362 | $this->builder->fields()->filter(function ($field) { |
||
| 363 | return $field instanceof Field\File; |
||
| 364 | })->each(function (Field\File $file) use ($data) { |
||
| 365 | $file->setOriginal($data); |
||
| 366 | |||
| 367 | $file->destroy(); |
||
| 368 | }); |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Store a new record. |
||
| 373 | * |
||
| 374 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse |
||
| 375 | */ |
||
| 376 | public function store() |
||
| 377 | { |
||
| 378 | $data = \request()->all(); |
||
| 379 | |||
| 380 | // Handle validation errors. |
||
| 381 | if ($validationMessages = $this->validationMessages($data)) { |
||
| 382 | return back()->withInput()->withErrors($validationMessages); |
||
| 383 | } |
||
| 384 | |||
| 385 | if (($response = $this->prepare($data)) instanceof Response) { |
||
| 386 | return $response; |
||
| 387 | } |
||
| 388 | |||
| 389 | View Code Duplication | DB::transaction(function () { |
|
| 390 | $inserts = $this->prepareInsert($this->updates); |
||
| 391 | |||
| 392 | foreach ($inserts as $column => $value) { |
||
| 393 | $this->model->setAttribute($column, $value); |
||
| 394 | } |
||
| 395 | |||
| 396 | $this->model->save(); |
||
| 397 | |||
| 398 | $this->updateRelation($this->relations); |
||
| 399 | }); |
||
| 400 | |||
| 401 | if (($response = $this->callSaved()) instanceof Response) { |
||
| 402 | return $response; |
||
| 403 | } |
||
| 404 | |||
| 405 | if ($response = $this->ajaxResponse(trans('admin.save_succeeded'))) { |
||
| 406 | return $response; |
||
| 407 | } |
||
| 408 | |||
| 409 | return $this->redirectAfterStore(); |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Get ajax response. |
||
| 414 | * |
||
| 415 | * @param string $message |
||
| 416 | * |
||
| 417 | * @return bool|\Illuminate\Http\JsonResponse |
||
| 418 | */ |
||
| 419 | protected function ajaxResponse($message) |
||
| 420 | { |
||
| 421 | $request = Request::capture(); |
||
| 422 | |||
| 423 | // ajax but not pjax |
||
| 424 | if ($request->ajax() && !$request->pjax()) { |
||
| 425 | return response()->json([ |
||
| 426 | 'status' => true, |
||
| 427 | 'message' => $message, |
||
| 428 | ]); |
||
| 429 | } |
||
| 430 | |||
| 431 | return false; |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Prepare input data for insert or update. |
||
| 436 | * |
||
| 437 | * @param array $data |
||
| 438 | * |
||
| 439 | * @return mixed |
||
| 440 | */ |
||
| 441 | protected function prepare($data = []) |
||
| 442 | { |
||
| 443 | if (($response = $this->callSubmitted()) instanceof Response) { |
||
| 444 | return $response; |
||
| 445 | } |
||
| 446 | |||
| 447 | $this->inputs = array_merge($this->removeIgnoredFields($data), $this->inputs); |
||
| 448 | |||
| 449 | if (($response = $this->callSaving()) instanceof Response) { |
||
| 450 | return $response; |
||
| 451 | } |
||
| 452 | |||
| 453 | $this->relations = $this->getRelationInputs($this->inputs); |
||
| 454 | |||
| 455 | $this->updates = Arr::except($this->inputs, array_keys($this->relations)); |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Remove ignored fields from input. |
||
| 460 | * |
||
| 461 | * @param array $input |
||
| 462 | * |
||
| 463 | * @return array |
||
| 464 | */ |
||
| 465 | protected function removeIgnoredFields($input) |
||
| 466 | { |
||
| 467 | Arr::forget($input, $this->ignored); |
||
| 468 | |||
| 469 | return $input; |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get inputs for relations. |
||
| 474 | * |
||
| 475 | * @param array $inputs |
||
| 476 | * |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | protected function getRelationInputs($inputs = []) |
||
| 480 | { |
||
| 481 | $relations = []; |
||
| 482 | |||
| 483 | foreach ($inputs as $column => $value) { |
||
| 484 | if (method_exists($this->model, $column)) { |
||
| 485 | $relation = call_user_func([$this->model, $column]); |
||
| 486 | |||
| 487 | if ($relation instanceof Relations\Relation) { |
||
| 488 | $relations[$column] = $value; |
||
| 489 | } |
||
| 490 | } |
||
| 491 | } |
||
| 492 | |||
| 493 | return $relations; |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Handle update. |
||
| 498 | * |
||
| 499 | * @param int $id |
||
| 500 | * @param null $data |
||
| 501 | * |
||
| 502 | * @return bool|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|mixed|null|Response |
||
| 503 | */ |
||
| 504 | public function update($id, $data = null) |
||
| 505 | { |
||
| 506 | $data = ($data) ?: request()->all(); |
||
| 507 | |||
| 508 | $isEditable = $this->isEditable($data); |
||
| 509 | |||
| 510 | if (($data = $this->handleColumnUpdates($id, $data)) instanceof Response) { |
||
| 511 | return $data; |
||
| 512 | } |
||
| 513 | |||
| 514 | /* @var Model $this->model */ |
||
| 515 | $builder = $this->model(); |
||
| 516 | |||
| 517 | if ($this->isSoftDeletes) { |
||
| 518 | $builder = $builder->withTrashed(); |
||
| 519 | } |
||
| 520 | |||
| 521 | $this->model = $builder->with($this->getRelations())->findOrFail($id); |
||
| 522 | |||
| 523 | $this->setFieldOriginalValue(); |
||
| 524 | |||
| 525 | // Handle validation errors. |
||
| 526 | if ($validationMessages = $this->validationMessages($data)) { |
||
| 527 | if (!$isEditable) { |
||
| 528 | return back()->withInput()->withErrors($validationMessages); |
||
| 529 | } |
||
| 530 | |||
| 531 | return response()->json(['errors' => Arr::dot($validationMessages->getMessages())], 422); |
||
| 532 | } |
||
| 533 | |||
| 534 | if (($response = $this->prepare($data)) instanceof Response) { |
||
| 535 | return $response; |
||
| 536 | } |
||
| 537 | |||
| 538 | View Code Duplication | DB::transaction(function () { |
|
| 539 | $updates = $this->prepareUpdate($this->updates); |
||
| 540 | |||
| 541 | foreach ($updates as $column => $value) { |
||
| 542 | /* @var Model $this->model */ |
||
| 543 | $this->model->setAttribute($column, $value); |
||
| 544 | } |
||
| 545 | |||
| 546 | $this->model->save(); |
||
| 547 | |||
| 548 | $this->updateRelation($this->relations); |
||
| 549 | }); |
||
| 550 | |||
| 551 | if (($result = $this->callSaved()) instanceof Response) { |
||
| 552 | return $result; |
||
| 553 | } |
||
| 554 | |||
| 555 | if ($response = $this->ajaxResponse(trans('admin.update_succeeded'))) { |
||
| 556 | return $response; |
||
| 557 | } |
||
| 558 | |||
| 559 | return $this->redirectAfterUpdate($id); |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Get RedirectResponse after store. |
||
| 564 | * |
||
| 565 | * @return \Illuminate\Http\RedirectResponse |
||
| 566 | */ |
||
| 567 | protected function redirectAfterStore() |
||
| 568 | { |
||
| 569 | $resourcesPath = $this->resource(0); |
||
| 570 | |||
| 571 | $key = $this->model->getKey(); |
||
| 572 | |||
| 573 | return $this->redirectAfterSaving($resourcesPath, $key); |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Get RedirectResponse after update. |
||
| 578 | * |
||
| 579 | * @param mixed $key |
||
| 580 | * |
||
| 581 | * @return \Illuminate\Http\RedirectResponse |
||
| 582 | */ |
||
| 583 | protected function redirectAfterUpdate($key) |
||
| 584 | { |
||
| 585 | $resourcesPath = $this->resource(-1); |
||
| 586 | |||
| 587 | return $this->redirectAfterSaving($resourcesPath, $key); |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Get RedirectResponse after data saving. |
||
| 592 | * |
||
| 593 | * @param string $resourcesPath |
||
| 594 | * @param string $key |
||
| 595 | * |
||
| 596 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
| 597 | */ |
||
| 598 | protected function redirectAfterSaving($resourcesPath, $key) |
||
| 599 | { |
||
| 600 | if (request('after-save') == 1) { |
||
| 601 | // continue editing |
||
| 602 | $url = rtrim($resourcesPath, '/')."/{$key}/edit"; |
||
| 603 | } elseif (request('after-save') == 2) { |
||
| 604 | // continue creating |
||
| 605 | $url = rtrim($resourcesPath, '/').'/create'; |
||
| 606 | } elseif (request('after-save') == 3) { |
||
| 607 | // view resource |
||
| 608 | $url = rtrim($resourcesPath, '/')."/{$key}"; |
||
| 609 | } else { |
||
| 610 | $url = request(Builder::PREVIOUS_URL_KEY) ?: $resourcesPath; |
||
| 611 | } |
||
| 612 | |||
| 613 | admin_toastr(trans('admin.save_succeeded')); |
||
| 614 | |||
| 615 | return redirect($url); |
||
| 616 | } |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Check if request is from editable. |
||
| 620 | * |
||
| 621 | * @param array $input |
||
| 622 | * |
||
| 623 | * @return bool |
||
| 624 | */ |
||
| 625 | protected function isEditable(array $input = []) |
||
| 626 | { |
||
| 627 | return array_key_exists('_editable', $input); |
||
| 628 | } |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Handle updates for single column. |
||
| 632 | * |
||
| 633 | * @param int $id |
||
| 634 | * @param array $data |
||
| 635 | * |
||
| 636 | * @return array|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|Response |
||
| 637 | */ |
||
| 638 | protected function handleColumnUpdates($id, $data) |
||
| 639 | { |
||
| 640 | $data = $this->handleEditable($data); |
||
| 641 | |||
| 642 | $data = $this->handleFileDelete($data); |
||
| 643 | |||
| 644 | $data = $this->handleFileSort($data); |
||
| 645 | |||
| 646 | if ($this->handleOrderable($id, $data)) { |
||
| 647 | return response([ |
||
| 648 | 'status' => true, |
||
| 649 | 'message' => trans('admin.update_succeeded'), |
||
| 650 | ]); |
||
| 651 | } |
||
| 652 | |||
| 653 | return $data; |
||
| 654 | } |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Handle editable update. |
||
| 658 | * |
||
| 659 | * @param array $input |
||
| 660 | * |
||
| 661 | * @return array |
||
| 662 | */ |
||
| 663 | protected function handleEditable(array $input = []) |
||
| 675 | |||
| 676 | /** |
||
| 677 | * @param array $input |
||
| 678 | * |
||
| 679 | * @return array |
||
| 680 | */ |
||
| 681 | protected function handleFileDelete(array $input = []) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * @param array $input |
||
| 695 | * |
||
| 696 | * @return array |
||
| 697 | */ |
||
| 698 | protected function handleFileSort(array $input = []) |
||
| 699 | { |
||
| 700 | if (!array_key_exists(Field::FILE_SORT_FLAG, $input)) { |
||
| 701 | return $input; |
||
| 702 | } |
||
| 703 | |||
| 704 | $sorts = array_filter($input[Field::FILE_SORT_FLAG]); |
||
| 705 | |||
| 706 | if (empty($sorts)) { |
||
| 707 | return $input; |
||
| 708 | } |
||
| 709 | |||
| 710 | foreach ($sorts as $column => $order) { |
||
| 711 | $input[$column] = $order; |
||
| 712 | } |
||
| 713 | |||
| 714 | request()->replace($input); |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Handle orderable update. |
||
| 721 | * |
||
| 722 | * @param int $id |
||
| 723 | * @param array $input |
||
| 724 | * |
||
| 725 | * @return bool |
||
| 726 | */ |
||
| 727 | protected function handleOrderable($id, array $input = []) |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Update relation data. |
||
| 744 | * |
||
| 745 | * @param array $relationsData |
||
| 746 | * |
||
| 747 | * @return void |
||
| 748 | */ |
||
| 749 | protected function updateRelation($relationsData) |
||
| 856 | |||
| 857 | /** |
||
| 858 | * Prepare input data for update. |
||
| 859 | * |
||
| 860 | * @param array $updates |
||
| 861 | * @param bool $oneToOneRelation If column is one-to-one relation. |
||
| 862 | * |
||
| 863 | * @return array |
||
| 864 | */ |
||
| 865 | protected function prepareUpdate(array $updates, $oneToOneRelation = false) |
||
| 897 | |||
| 898 | /** |
||
| 899 | * @param string|array $columns |
||
| 900 | * @param bool $oneToOneRelation |
||
| 901 | * |
||
| 902 | * @return bool |
||
| 903 | */ |
||
| 904 | protected function invalidColumn($columns, $oneToOneRelation = false) |
||
| 915 | |||
| 916 | /** |
||
| 917 | * Prepare input data for insert. |
||
| 918 | * |
||
| 919 | * @param $inserts |
||
| 920 | * |
||
| 921 | * @return array |
||
| 922 | */ |
||
| 923 | protected function prepareInsert($inserts) |
||
| 946 | |||
| 947 | /** |
||
| 948 | * Is input data is has-one relation. |
||
| 949 | * |
||
| 950 | * @param array $inserts |
||
| 951 | * |
||
| 952 | * @return bool |
||
| 953 | */ |
||
| 954 | protected function isHasOneRelation($inserts) |
||
| 968 | |||
| 969 | /** |
||
| 970 | * Ignore fields to save. |
||
| 971 | * |
||
| 972 | * @param string|array $fields |
||
| 973 | * |
||
| 974 | * @return $this |
||
| 975 | */ |
||
| 976 | public function ignore($fields) |
||
| 982 | |||
| 983 | /** |
||
| 984 | * @param array $data |
||
| 985 | * @param string|array $columns |
||
| 986 | * |
||
| 987 | * @return array|mixed |
||
| 988 | */ |
||
| 989 | View Code Duplication | protected function getDataByColumn($data, $columns) |
|
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Find field object by column. |
||
| 1010 | * |
||
| 1011 | * @param $column |
||
| 1012 | * |
||
| 1013 | * @return mixed |
||
| 1014 | */ |
||
| 1015 | protected function getFieldByColumn($column) |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * Set original data for each field. |
||
| 1030 | * |
||
| 1031 | * @return void |
||
| 1032 | */ |
||
| 1033 | protected function setFieldOriginalValue() |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * Set all fields value in form. |
||
| 1046 | * |
||
| 1047 | * @param $id |
||
| 1048 | * |
||
| 1049 | * @return void |
||
| 1050 | */ |
||
| 1051 | protected function setFieldValue($id) |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Don't snake case attributes. |
||
| 1078 | * |
||
| 1079 | * @param Model $model |
||
| 1080 | * |
||
| 1081 | * @return void |
||
| 1082 | */ |
||
| 1083 | protected static function doNotSnakeAttributes(Model $model) |
||
| 1089 | |||
| 1090 | /** |
||
| 1091 | * Get validation messages. |
||
| 1092 | * |
||
| 1093 | * @param array $input |
||
| 1094 | * |
||
| 1095 | * @return MessageBag|bool |
||
| 1096 | */ |
||
| 1097 | public function validationMessages($input) |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * Merge validation messages from input validators. |
||
| 1119 | * |
||
| 1120 | * @param \Illuminate\Validation\Validator[] $validators |
||
| 1121 | * |
||
| 1122 | * @return MessageBag |
||
| 1123 | */ |
||
| 1124 | protected function mergeValidationMessages($validators) |
||
| 1134 | |||
| 1135 | /** |
||
| 1136 | * Get all relations of model from callable. |
||
| 1137 | * |
||
| 1138 | * @return array |
||
| 1139 | */ |
||
| 1140 | public function getRelations() |
||
| 1167 | |||
| 1168 | /** |
||
| 1169 | * Set action for form. |
||
| 1170 | * |
||
| 1171 | * @param string $action |
||
| 1172 | * |
||
| 1173 | * @return $this |
||
| 1174 | */ |
||
| 1175 | public function setAction($action) |
||
| 1181 | |||
| 1182 | /** |
||
| 1183 | * Set field and label width in current form. |
||
| 1184 | * |
||
| 1185 | * @param int $fieldWidth |
||
| 1186 | * @param int $labelWidth |
||
| 1187 | * |
||
| 1188 | * @return $this |
||
| 1189 | */ |
||
| 1190 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
| 1201 | |||
| 1202 | /** |
||
| 1203 | * Set view for form. |
||
| 1204 | * |
||
| 1205 | * @param string $view |
||
| 1206 | * |
||
| 1207 | * @return $this |
||
| 1208 | */ |
||
| 1209 | public function setView($view) |
||
| 1215 | |||
| 1216 | /** |
||
| 1217 | * Set title for form. |
||
| 1218 | * |
||
| 1219 | * @param string $title |
||
| 1220 | * |
||
| 1221 | * @return $this |
||
| 1222 | */ |
||
| 1223 | public function setTitle($title = '') |
||
| 1229 | |||
| 1230 | /** |
||
| 1231 | * Add a row in form. |
||
| 1232 | * |
||
| 1233 | * @param Closure $callback |
||
| 1234 | * |
||
| 1235 | * @return $this |
||
| 1236 | */ |
||
| 1237 | public function row(Closure $callback) |
||
| 1243 | |||
| 1244 | /** |
||
| 1245 | * Tools setting for form. |
||
| 1246 | * |
||
| 1247 | * @param Closure $callback |
||
| 1248 | */ |
||
| 1249 | public function tools(Closure $callback) |
||
| 1253 | |||
| 1254 | /** |
||
| 1255 | * Disable form submit. |
||
| 1256 | * |
||
| 1257 | * @param bool $disable |
||
| 1258 | * |
||
| 1259 | * @return $this |
||
| 1260 | * |
||
| 1261 | * @deprecated |
||
| 1262 | */ |
||
| 1263 | public function disableSubmit(bool $disable = true) |
||
| 1269 | |||
| 1270 | /** |
||
| 1271 | * Disable form reset. |
||
| 1272 | * |
||
| 1273 | * @param bool $disable |
||
| 1274 | * |
||
| 1275 | * @return $this |
||
| 1276 | * |
||
| 1277 | * @deprecated |
||
| 1278 | */ |
||
| 1279 | public function disableReset(bool $disable = true) |
||
| 1285 | |||
| 1286 | /** |
||
| 1287 | * Disable View Checkbox on footer. |
||
| 1288 | * |
||
| 1289 | * @param bool $disable |
||
| 1290 | * |
||
| 1291 | * @return $this |
||
| 1292 | */ |
||
| 1293 | public function disableViewCheck(bool $disable = true) |
||
| 1299 | |||
| 1300 | /** |
||
| 1301 | * Disable Editing Checkbox on footer. |
||
| 1302 | * |
||
| 1303 | * @param bool $disable |
||
| 1304 | * |
||
| 1305 | * @return $this |
||
| 1306 | */ |
||
| 1307 | public function disableEditingCheck(bool $disable = true) |
||
| 1313 | |||
| 1314 | /** |
||
| 1315 | * Disable Creating Checkbox on footer. |
||
| 1316 | * |
||
| 1317 | * @param bool $disable |
||
| 1318 | * |
||
| 1319 | * @return $this |
||
| 1320 | */ |
||
| 1321 | public function disableCreatingCheck(bool $disable = true) |
||
| 1327 | |||
| 1328 | /** |
||
| 1329 | * Footer setting for form. |
||
| 1330 | * |
||
| 1331 | * @param Closure $callback |
||
| 1332 | */ |
||
| 1333 | public function footer(Closure $callback) |
||
| 1337 | |||
| 1338 | /** |
||
| 1339 | * Get current resource route url. |
||
| 1340 | * |
||
| 1341 | * @param int $slice |
||
| 1342 | * |
||
| 1343 | * @return string |
||
| 1344 | */ |
||
| 1345 | public function resource($slice = -2) |
||
| 1355 | |||
| 1356 | /** |
||
| 1357 | * Render the form contents. |
||
| 1358 | * |
||
| 1359 | * @return string |
||
| 1360 | */ |
||
| 1361 | public function render() |
||
| 1369 | |||
| 1370 | /** |
||
| 1371 | * Get or set input data. |
||
| 1372 | * |
||
| 1373 | * @param string $key |
||
| 1374 | * @param null $value |
||
| 1375 | * |
||
| 1376 | * @return array|mixed |
||
| 1377 | */ |
||
| 1378 | public function input($key, $value = null) |
||
| 1386 | |||
| 1387 | /** |
||
| 1388 | * Register builtin fields. |
||
| 1389 | * |
||
| 1390 | * @return void |
||
| 1391 | */ |
||
| 1392 | public static function registerBuiltinFields() |
||
| 1448 | |||
| 1449 | /** |
||
| 1450 | * Register custom field. |
||
| 1451 | * |
||
| 1452 | * @param string $abstract |
||
| 1453 | * @param string $class |
||
| 1454 | * |
||
| 1455 | * @return void |
||
| 1456 | */ |
||
| 1457 | public static function extend($abstract, $class) |
||
| 1461 | |||
| 1462 | /** |
||
| 1463 | * Set form field alias. |
||
| 1464 | * |
||
| 1465 | * @param string $field |
||
| 1466 | * @param string $alias |
||
| 1467 | * |
||
| 1468 | * @return void |
||
| 1469 | */ |
||
| 1470 | public static function alias($field, $alias) |
||
| 1474 | |||
| 1475 | /** |
||
| 1476 | * Remove registered field. |
||
| 1477 | * |
||
| 1478 | * @param array|string $abstract |
||
| 1479 | */ |
||
| 1480 | public static function forget($abstract) |
||
| 1484 | |||
| 1485 | /** |
||
| 1486 | * Find field class. |
||
| 1487 | * |
||
| 1488 | * @param string $method |
||
| 1489 | * |
||
| 1490 | * @return bool|mixed |
||
| 1491 | */ |
||
| 1492 | public static function findFieldClass($method) |
||
| 1507 | |||
| 1508 | /** |
||
| 1509 | * Collect assets required by registered field. |
||
| 1510 | * |
||
| 1511 | * @return array |
||
| 1512 | */ |
||
| 1513 | public static function collectFieldAssets() |
||
| 1538 | |||
| 1539 | /** |
||
| 1540 | * Getter. |
||
| 1541 | * |
||
| 1542 | * @param string $name |
||
| 1543 | * |
||
| 1544 | * @return array|mixed |
||
| 1545 | */ |
||
| 1546 | public function __get($name) |
||
| 1550 | |||
| 1551 | /** |
||
| 1552 | * Setter. |
||
| 1553 | * |
||
| 1554 | * @param string $name |
||
| 1555 | * @param mixed $value |
||
| 1556 | * @return array |
||
| 1557 | */ |
||
| 1558 | public function __set($name, $value) |
||
| 1562 | |||
| 1563 | /** |
||
| 1564 | * Generate a Field object and add to form builder if Field exists. |
||
| 1565 | * |
||
| 1566 | * @param string $method |
||
| 1567 | * @param array $arguments |
||
| 1568 | * |
||
| 1569 | * @return Field |
||
| 1570 | */ |
||
| 1571 | public function __call($method, $arguments) |
||
| 1587 | } |
||
| 1588 |
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: