| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Encore\Admin\Form\Field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Encore\Admin\Admin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Encore\Admin\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Encore\Admin\Form\Field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Encore\Admin\Form\NestedForm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Database\Eloquent\Relations\HasMany as Relation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Database\Eloquent\Relations\MorphMany; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Illuminate\Support\Facades\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Class HasMany. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class HasMany extends Field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * Relation name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $relationName = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * Form builder. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var \Closure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected $builder = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * Form data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     protected $value = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * View Mode. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Supports `default` and `tab` currently. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected $viewMode = 'default'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * Available views for HasMany field. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     protected $views = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         'default' => 'admin::form.hasmany', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         'tab'     => 'admin::form.hasmanytab', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * Create a new HasMany field instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param $relationName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param array $arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 65 |  | View Code Duplication |     public function __construct($relationName, $arguments = []) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->relationName = $relationName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->column = $relationName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if (count($arguments) == 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $this->label = $this->formatLabel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $this->builder = $arguments[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if (count($arguments) == 2) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             list($this->label, $this->builder) = $arguments; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * Get validator for this field. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param array $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @return bool|Validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function getValidator(array $input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         if (!array_key_exists($this->column, $input)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $input = array_only($input, $this->column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $form = $this->buildNestedForm($this->column, $this->builder); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $rules = $attributes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         /* @var Field $field */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         foreach ($form->fields() as $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             if (!$fieldRules = $field->getRules()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $column = $field->column(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             if (is_array($column)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 foreach ($column as $key => $name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     $rules[$name.$key] = $fieldRules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 $this->resetInputKey($input, $column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 $rules[$column] = $fieldRules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $attributes = array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 $attributes, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 $this->formatValidationAttribute($input, $field->label(), $column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         array_forget($rules, NestedForm::REMOVE_FLAG_NAME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         if (empty($rules)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $newRules = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $newInput = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         foreach ($rules as $column => $rule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             foreach (array_keys($input[$this->column]) as $key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 $newRules["{$this->column}.$key.$column"] = $rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 if (isset($input[$this->column][$key][$column])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                     foreach ($input[$this->column][$key][$column] as $vkey => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                         $newInput["{$this->column}.$key.{$column}$vkey"] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         return Validator::make($newInput, $newRules, $this->validationMessages, $attributes); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * Format validation attributes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param array  $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @param string $label | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @param string $column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 156 |  | View Code Duplication |     protected function formatValidationAttribute($input, $label, $column) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $new = $attributes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         if (is_array($column)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             foreach ($column as $index => $col) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |                 $new[$col.$index] = $col; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         foreach (array_keys(array_dot($input)) as $key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             if (is_string($column)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                 if (Str::endsWith($key, ".$column")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                     $attributes[$key] = $label; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |                 foreach ($new as $k => $val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |                     if (Str::endsWith($key, ".$k")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                         $attributes[$key] = $label."[$val]"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         return $attributes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      * Reset input key for validation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |      * @param array $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |      * @param array $column $column is the column name array set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      * @return void. | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 190 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 191 |  |  |     protected function resetInputKey(array &$input, array $column) | 
            
                                                                        
                            
            
                                    
            
            
                | 192 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 193 |  |  |         /** | 
            
                                                                        
                            
            
                                    
            
            
                | 194 |  |  |          * flip the column name array set. | 
            
                                                                        
                            
            
                                    
            
            
                | 195 |  |  |          * | 
            
                                                                        
                            
            
                                    
            
            
                | 196 |  |  |          * for example, for the DateRange, the column like as below | 
            
                                                                        
                            
            
                                    
            
            
                | 197 |  |  |          * | 
            
                                                                        
                            
            
                                    
            
            
                | 198 |  |  |          * ["start" => "created_at", "end" => "updated_at"] | 
            
                                                                        
                            
            
                                    
            
            
                | 199 |  |  |          * | 
            
                                                                        
                            
            
                                    
            
            
                | 200 |  |  |          * to: | 
            
                                                                        
                            
            
                                    
            
            
                | 201 |  |  |          * | 
            
                                                                        
                            
            
                                    
            
            
                | 202 |  |  |          * [ "created_at" => "start", "updated_at" => "end" ] | 
            
                                                                        
                            
            
                                    
            
            
                | 203 |  |  |          */ | 
            
                                                                        
                            
            
                                    
            
            
                | 204 |  |  |         $column = array_flip($column); | 
            
                                                                        
                            
            
                                    
            
            
                | 205 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 206 |  |  |         /** | 
            
                                                                        
                            
            
                                    
            
            
                | 207 |  |  |          * $this->column is the inputs array's node name, default is the relation name. | 
            
                                                                        
                            
            
                                    
            
            
                | 208 |  |  |          * | 
            
                                                                        
                            
            
                                    
            
            
                | 209 |  |  |          * So... $input[$this->column] is the data of this column's inputs data | 
            
                                                                        
                            
            
                                    
            
            
                | 210 |  |  |          * | 
            
                                                                        
                            
            
                                    
            
            
                | 211 |  |  |          * in the HasMany relation, has many data/field set, $set is field set in the below | 
            
                                                                        
                            
            
                                    
            
            
                | 212 |  |  |          */ | 
            
                                                                        
                            
            
                                    
            
            
                | 213 |  |  |         foreach ($input[$this->column] as $index => $set) { | 
            
                                                                        
                            
            
                                    
            
            
                | 214 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 215 |  |  |             /* | 
            
                                                                        
                            
            
                                    
            
            
                | 216 |  |  |              * foreach the field set to find the corresponding $column | 
            
                                                                        
                            
            
                                    
            
            
                | 217 |  |  |              */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 218 |  | View Code Duplication |             foreach ($set as $name => $value) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 219 |  |  |                 /* | 
            
                                                                        
                            
            
                                    
            
            
                | 220 |  |  |                  * if doesn't have column name, continue to the next loop | 
            
                                                                        
                            
            
                                    
            
            
                | 221 |  |  |                  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 222 |  |  |                 if (!array_key_exists($name, $column)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 223 |  |  |                     continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 224 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 225 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 226 |  |  |                 /** | 
            
                                                                        
                            
            
                                    
            
            
                | 227 |  |  |                  * example:  $newKey = created_atstart. | 
            
                                                                        
                            
            
                                    
            
            
                | 228 |  |  |                  * | 
            
                                                                        
                            
            
                                    
            
            
                | 229 |  |  |                  * Σ( ° △ °|||)︴ | 
            
                                                                        
                            
            
                                    
            
            
                | 230 |  |  |                  * | 
            
                                                                        
                            
            
                                    
            
            
                | 231 |  |  |                  * I don't know why a form need range input? Only can imagine is for range search.... | 
            
                                                                        
                            
            
                                    
            
            
                | 232 |  |  |                  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 233 |  |  |                 $newKey = $name.$column[$name]; | 
            
                                                                        
                            
            
                                    
            
            
                | 234 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 235 |  |  |                 /* | 
            
                                                                        
                            
            
                                    
            
            
                | 236 |  |  |                  * set new key | 
            
                                                                        
                            
            
                                    
            
            
                | 237 |  |  |                  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 238 |  |  |                 array_set($input, "{$this->column}.$index.$newKey", $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 239 |  |  |                 /* | 
            
                                                                        
                            
            
                                    
            
            
                | 240 |  |  |                  * forget the old key and value | 
            
                                                                        
                            
            
                                    
            
            
                | 241 |  |  |                  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 242 |  |  |                 array_forget($input, "{$this->column}.$index.$name"); | 
            
                                                                        
                            
            
                                    
            
            
                | 243 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 244 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 245 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |      * Prepare input data for insert or update. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |      * @param array $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |     public function prepare($input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |         $form = $this->buildNestedForm($this->column, $this->builder); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |         return $form->setOriginal($this->original, $this->getKeyName())->prepare($input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |      * Build a Nested form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |      * @param string   $column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |      * @param \Closure $builder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |      * @param null     $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |      * @return NestedForm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |     protected function buildNestedForm($column, \Closure $builder, $key = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |         $form = new Form\NestedForm($column, $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |         $form->setForm($this->form); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |         call_user_func($builder, $form); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |         $form->hidden($this->getKeyName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |         $form->hidden(NestedForm::REMOVE_FLAG_NAME)->default(0)->addElementClass(NestedForm::REMOVE_FLAG_CLASS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |         return $form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |      * Get the HasMany relation key name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |     protected function getKeyName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |         if (is_null($this->form)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |         return $this->form->model()->{$this->relationName}()->getRelated()->getKeyName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |      * Set view mode. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |      * @param string $mode currently support `tab` mode. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |      * @author Edwin Hui | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |     public function mode($mode) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |         $this->viewMode = $mode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |      * Use tab mode to showing hasmany field. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |      * @return HasMany | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |     public function useTab() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |         return $this->mode('tab'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |      * Build Nested form for related data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |     protected function buildRelatedForms() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |         if (is_null($this->form)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |         $model = $this->form->model(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |         $relation = call_user_func([$model, $this->relationName]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |         if (!$relation instanceof Relation && !$relation instanceof MorphMany) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |             throw new \Exception('hasMany field must be a HasMany or MorphMany relation.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |         $forms = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |          * If redirect from `exception` or `validation error` page. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |          * Then get form data from session flash. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |          * Else get data from database. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |         if ($values = old($this->column)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |             foreach ($values as $key => $data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |                 if ($data[NestedForm::REMOVE_FLAG_NAME] == 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |                 $forms[$key] = $this->buildNestedForm($this->column, $this->builder, $key) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  |                     ->fill($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  |             foreach ($this->value as $data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  |                 $key = array_get($data, $relation->getRelated()->getKeyName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  |                 $forms[$key] = $this->buildNestedForm($this->column, $this->builder, $key) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |                     ->fill($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  |         return $forms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |      * Setup script for this field in different view mode. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  |      * @param string $script | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |     protected function setupScript($script) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |         $method = 'setupScriptFor'.ucfirst($this->viewMode).'View'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |         call_user_func([$this, $method], $script); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |      * Setup default template script. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |      * @param string $templateScript | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 397 |  | View Code Duplication |     protected function setupScriptForDefaultView($templateScript) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  |         $removeClass = NestedForm::REMOVE_FLAG_CLASS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 |  |  |         $defaultKey = NestedForm::DEFAULT_KEY_NAME; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 |  |  |          * When add a new sub form, replace all element key in new sub form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 |  |  |          * @example comments[new___key__][title]  => comments[new_{index}][title] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  |          * {count} is increment number of current sub form count. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 |  |  |         $script = <<<EOT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  | var index = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 411 |  |  | $('#has-many-{$this->column}').on('click', '.add', function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 412 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 413 |  |  |     var tpl = $('template.{$this->column}-tpl'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 414 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 415 |  |  |     index++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 416 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 417 |  |  |     var template = tpl.html().replace(/{$defaultKey}/g, index); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 418 |  |  |     $('.has-many-{$this->column}-forms').append(template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 419 |  |  |     {$templateScript} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 420 |  |  | }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 421 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 422 |  |  | $('#has-many-{$this->column}').on('click', '.remove', function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 423 |  |  |     $(this).closest('.has-many-{$this->column}-form').hide(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 424 |  |  |     $(this).closest('.has-many-{$this->column}-form').find('.$removeClass').val(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 425 |  |  | }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 426 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 427 |  |  | EOT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 428 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 429 |  |  |         Admin::script($script); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 430 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 431 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 432 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 433 |  |  |      * Setup tab template script. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 434 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 435 |  |  |      * @param string $templateScript | 
            
                                                                                                            
                            
            
                                    
            
            
                | 436 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 437 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 438 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 439 |  | View Code Duplication |     protected function setupScriptForTabView($templateScript) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 440 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 441 |  |  |         $removeClass = NestedForm::REMOVE_FLAG_CLASS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 442 |  |  |         $defaultKey = NestedForm::DEFAULT_KEY_NAME; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 443 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 444 |  |  |         $script = <<<EOT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 445 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 446 |  |  | $('#has-many-{$this->column} > .nav').off('click', 'i.close-tab').on('click', 'i.close-tab', function(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 447 |  |  |     var \$navTab = $(this).siblings('a'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 448 |  |  |     var \$pane = $(\$navTab.attr('href')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 449 |  |  |     if( \$pane.hasClass('new') ){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 450 |  |  |         \$pane.remove(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 451 |  |  |     }else{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 452 |  |  |         \$pane.removeClass('active').find('.$removeClass').val(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 453 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 454 |  |  |     if(\$navTab.closest('li').hasClass('active')){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 455 |  |  |         \$navTab.closest('li').remove(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 456 |  |  |         $('#has-many-{$this->column} > .nav > li:nth-child(1) > a').tab('show'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 457 |  |  |     }else{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 458 |  |  |         \$navTab.closest('li').remove(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 459 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 460 |  |  | }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 461 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 462 |  |  | var index = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 463 |  |  | $('#has-many-{$this->column} > .header').off('click', '.add').on('click', '.add', function(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 464 |  |  |     index++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 465 |  |  |     var navTabHtml = $('#has-many-{$this->column} > template.nav-tab-tpl').html().replace(/{$defaultKey}/g, index); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 466 |  |  |     var paneHtml = $('#has-many-{$this->column} > template.pane-tpl').html().replace(/{$defaultKey}/g, index); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 467 |  |  |     $('#has-many-{$this->column} > .nav').append(navTabHtml); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 468 |  |  |     $('#has-many-{$this->column} > .tab-content').append(paneHtml); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 469 |  |  |     $('#has-many-{$this->column} > .nav > li:last-child a').tab('show'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 470 |  |  |     {$templateScript} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 471 |  |  | }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 472 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 473 |  |  | if ($('.has-error').length) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 474 |  |  |     $('.has-error').parent('.tab-pane').each(function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 475 |  |  |         var tabId = '#'+$(this).attr('id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 476 |  |  |         $('li a[href="'+tabId+'"] i').removeClass('hide'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 477 |  |  |     }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 478 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 479 |  |  |     var first = $('.has-error:first').parent().attr('id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 480 |  |  |     $('li a[href="#'+first+'"]').tab('show'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 481 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 482 |  |  | EOT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 483 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 484 |  |  |         Admin::script($script); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 485 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 486 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 487 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 488 |  |  |      * Render the `HasMany` field. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 489 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 490 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 491 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 492 |  |  |      * @return \Illuminate\View\View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 493 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 494 |  |  |     public function render() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 495 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 496 |  |  |         // specify a view to render. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 497 |  |  |         $this->view = $this->views[$this->viewMode]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 498 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 499 |  |  |         list($template, $script) = $this->buildNestedForm($this->column, $this->builder) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 500 |  |  |             ->getTemplateHtmlAndScript(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 501 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 502 |  |  |         $this->setupScript($script); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 503 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 504 |  |  |         return parent::render()->with([ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 505 |  |  |             'forms'        => $this->buildRelatedForms(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 506 |  |  |             'template'     => $template, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 507 |  |  |             'relationName' => $this->relationName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 508 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 509 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 510 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 511 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.