| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Anavel\Crud\Abstractor\Eloquent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Anavel\Crud\Abstractor\Eloquent\Traits\ModelFields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Anavel\Crud\Contracts\Abstractor\Model as ModelAbstractorContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Anavel\Crud\Abstractor\ConfigurationReader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Anavel\Crud\Contracts\Abstractor\Relation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Anavel\Crud\Contracts\Abstractor\RelationFactory as RelationFactoryContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Anavel\Crud\Contracts\Abstractor\FieldFactory as FieldFactoryContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use FormManager\ElementInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Illuminate\Database\Eloquent\Model as LaravelModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use App; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Anavel\Crud\Contracts\Form\Generator as FormGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Anavel\Crud\Abstractor\Exceptions\AbstractorException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Illuminate\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Illuminate\Support\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use League\Flysystem\Adapter\Local; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use League\Flysystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class Model implements ModelAbstractorContract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     use ConfigurationReader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     use ModelFields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $dbal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $relationFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $fieldFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $generator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected $slug; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected $instance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 17 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         AbstractionLayer $dbal, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         RelationFactoryContract $relationFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         FieldFactoryContract $fieldFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         FormGenerator $generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 17 |  |         if (is_array($config)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 17 |  |             $this->model = $config['model']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 17 |  |             $this->config = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 17 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $this->model = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $this->config = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 17 |  |         $this->dbal = $dbal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 17 |  |         $this->relationFactory = $relationFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 17 |  |         $this->fieldFactory = $fieldFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 17 |  |         $this->generator = $generator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 17 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 3 |  |     public function setSlug($slug) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  |         $this->slug = $slug; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |     public function setName($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |         $this->name = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 5 |  |     public function setInstance($instance) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 5 |  |         $this->instance = $instance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 5 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public function getSlug() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $this->slug; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function getName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return transcrud($this->name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |     public function getModel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |         return $this->model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function getInstance() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return $this->instance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     public function isSoftDeletes() | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         return $this->getConfigValue('soft_deletes') ? true : false; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 9 |  |     public function getColumns($action, $withForeignKeys = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 9 |  |         $tableColumns = $this->dbal->getTableColumns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 9 |  |         $filteredColumns = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 9 |  |         foreach ($tableColumns as $name => $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 9 |  |             $filteredColumns[str_replace('`', '', $name)] = $column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 9 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 9 |  |         $tableColumns = $filteredColumns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 9 |  |         $foreignKeysName = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 9 |  |         if ($withForeignKeys === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 8 |  |             $foreignKeys = $this->dbal->getTableForeignKeys(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 8 |  |             foreach ($foreignKeys as $foreignKey) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 foreach ($foreignKey->getColumns() as $columnName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                     $foreignKeysName[] = $columnName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 8 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 8 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 9 |  |         $customDisplayedColumns = $this->getConfigValue($action, 'display'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 9 |  |         $customHiddenColumns = $this->getConfigValue($action, 'hide') ? : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 9 |  |         $columns = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 9 |  |         if (! empty($customDisplayedColumns) && is_array($customDisplayedColumns)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 9 |  |             foreach ($customDisplayedColumns as $customColumn) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 9 |  |                 if (! array_key_exists($customColumn, $tableColumns)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                     throw new AbstractorException("Column " . $customColumn . " does not exist on " . $this->getModel()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 9 |  |                 $columns[$customColumn] = $tableColumns[$customColumn]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 9 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 9 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             foreach ($tableColumns as $name => $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 if (in_array($name, $customHiddenColumns)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                 if (in_array($name, $foreignKeysName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                 $columns[$name] = $column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 9 |  |         return $columns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @return \Illuminate\Support\Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 5 |  |     public function getRelations() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 5 |  |         $configRelations = $this->getConfigValue('relations'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 5 |  |         $relations = collect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 5 |  |         if (! empty($configRelations)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 5 |  |             foreach ($configRelations as $relationName => $configRelation) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 5 |  |                 if (is_int($relationName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                     $relationName = $configRelation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 5 |  |                 $config = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 5 |  |                 if ($configRelation !== $relationName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 5 |  |                     if (! is_array($configRelation)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                         $config['type'] = $configRelation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 5 |  |                         $config = $configRelation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 5 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |                 /** @var Relation $relation */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 5 |  |                 $relation = $this->relationFactory->setModel($this->instance) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 5 |  |                     ->setConfig($config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 5 |  |                     ->get($relationName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 5 |  |                 $secondaryRelations = $relation->getSecondaryRelations(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 5 |  |                 if (! $secondaryRelations->isEmpty()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 | 1 |  |                     $relations->put($relationName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 1 |  |                         collect(['relation' => $relation, 'secondaryRelations' => $secondaryRelations])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 | 1 |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 4 |  |                     $relations->put($relationName, $relation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 | 5 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 5 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 5 |  |         return $relations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |      * @param string|null $arrayKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |      * @throws AbstractorException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 206 | 2 | View Code Duplication |     public function getListFields($arrayKey = 'main') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 | 2 |  |         $columns = $this->getColumns('list'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 | 2 |  |         $fieldsPresentation = $this->getConfigValue('fields_presentation') ? : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 | 2 |  |         $fields = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 | 2 |  |         foreach ($columns as $name => $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 | 2 |  |             $presentation = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 | 2 |  |             if (array_key_exists($name, $fieldsPresentation)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 | 2 |  |                 $presentation = $fieldsPresentation[$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 | 2 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |             $config = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 | 2 |  |                 'name'         => $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 | 2 |  |                 'presentation' => $presentation, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 | 2 |  |                 'form_type'    => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 | 2 |  |                 'validation'   => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |                 'functions'    => null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 | 2 |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 | 2 |  |             $fields[$arrayKey][] = $this->fieldFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 | 2 |  |                 ->setColumn($column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 | 2 |  |                 ->setConfig($config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 | 2 |  |                 ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 | 2 |  |         return $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |      * @param string|null $arrayKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |      * @throws AbstractorException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 241 | 2 | View Code Duplication |     public function getDetailFields($arrayKey = 'main') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 | 2 |  |         $columns = $this->getColumns('detail'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 | 2 |  |         $fieldsPresentation = $this->getConfigValue('fields_presentation') ? : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 | 2 |  |         $fields = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 | 2 |  |         foreach ($columns as $name => $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 | 2 |  |             $presentation = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 | 2 |  |             if (array_key_exists($name, $fieldsPresentation)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 | 2 |  |                 $presentation = $fieldsPresentation[$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 | 2 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |             $config = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 | 2 |  |                 'name'         => $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 | 2 |  |                 'presentation' => $presentation, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 | 2 |  |                 'form_type'    => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 | 2 |  |                 'validation'   => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |                 'functions'    => null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 | 2 |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 | 2 |  |             $fields[$arrayKey][] = $this->fieldFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 | 2 |  |                 ->setColumn($column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 | 2 |  |                 ->setConfig($config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 | 2 |  |                 ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 | 2 |  |         return $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |      * @param bool|null $withForeignKeys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |      * @param string|null $arrayKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |      * @throws AbstractorException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 | 5 |  |     public function getEditFields($withForeignKeys = false, $arrayKey = 'main') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 | 5 |  |         $columns = $this->getColumns('edit', $withForeignKeys); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 | 5 |  |         $this->readConfig('edit'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 | 5 |  |         $fields = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 | 5 |  |         foreach ($columns as $name => $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 | 5 |  |             if (! in_array($name, $this->getReadOnlyColumns())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 | 5 |  |                 $presentation = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 | 5 |  |                 if (array_key_exists($name, $this->fieldsPresentation)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |                     $presentation = $this->fieldsPresentation[$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |                 $config = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 | 5 |  |                     'name'         => $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 | 5 |  |                     'presentation' => $presentation, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 | 5 |  |                     'form_type'    => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 | 5 |  |                     'validation'   => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |                     'functions'    => null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 | 5 |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 | 5 |  |                 $config = $this->setConfig($config, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 | 5 |  |                 $field = $this->fieldFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 | 5 |  |                     ->setColumn($column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 | 5 |  |                     ->setConfig($config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 | 5 |  |                     ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 | 5 |  |                 if (! empty($this->instance) && ! empty($this->instance->getAttribute($name))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |                     $field->setValue($this->instance->getAttribute($name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 | 5 |  |                 $fields[$arrayKey][] = $field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 | 5 |  |                 if (! empty($config['form_type']) && $config['form_type'] === 'file') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 | 5 |  |                     $field = $this->fieldFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 | 5 |  |                         ->setColumn($column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 | 5 |  |                         ->setConfig([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 | 5 |  |                             'name'         => $name . '__delete', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 | 5 |  |                             'presentation' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 | 5 |  |                             'form_type'    => 'checkbox', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 | 5 |  |                             'validation'   => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |                             'functions'    => null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 | 5 |  |                         ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 | 5 |  |                         ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 | 5 |  |                     $fields[$arrayKey][] = $field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 | 5 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 | 5 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 | 5 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 | 5 |  |         return $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 | 5 |  |     protected function getReadOnlyColumns() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 | 5 |  |         $columns = [LaravelModel::CREATED_AT, LaravelModel::UPDATED_AT]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 | 5 |  |         $columns[] = $this->dbal->getModel()->getKeyName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 | 5 |  |         return $columns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |      * @param string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |      * @return ElementInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 | 2 |  |     public function getForm($action) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 | 2 |  |         $this->generator->setModelFields($this->getEditFields()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 | 2 |  |         $this->generator->setRelatedModelFields($this->getRelations()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 | 2 |  |         return $this->generator->getForm($action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |      * @param array $requestForm | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 | 1 |  |     public function persist(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |         /** @var \ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager $modelManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 | 1 |  |         $modelManager = App::make('ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 | 1 |  |         if (! empty($this->instance)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |             $item = $this->instance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 | 1 |  |             $item = $modelManager->getModelInstance($this->getModel()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 | 1 |  |         $fields = $this->getEditFields(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 | 1 |  |         if (empty($fields['main']) && empty($this->getRelations())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 | 1 |  |         if (! empty($fields['main'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 | 1 |  |             $skipNext = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 | 1 |  |             foreach ($fields['main'] as $key => $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 | 1 |  |                 if ($skipNext === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 | 1 |  |                 $fieldName = $field->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 | 1 |  |                 $requestValue = $request->input("main.{$fieldName}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 | 1 |  |                 if (get_class($field->getFormField()) === \FormManager\Fields\File::class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |                     $modelFolder = $this->slug . DIRECTORY_SEPARATOR; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |                     $basePath = base_path(config('anavel-crud.uploads_path')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |                     $modelPath = $basePath . $modelFolder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |                     if (! empty($fields['main'][$key + 1]) && $fields['main'][$key + 1]->getName() === $fieldName . '__delete') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |                         //We never want to save this field, it doesn't exist in the DB | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |                         $skipNext = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |                         //If user wants to delete the existing file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |                         if (! empty($request->input("main.{$fieldName}__delete"))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |                             $adapter = new Local($basePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  |                             $filesystem = new Filesystem($adapter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |                             if ($filesystem->has($item->$fieldName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  |                                 $filesystem->delete($item->$fieldName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  |                             $item->setAttribute( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  |                                 $fieldName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 |  |  |                                 null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 |  |  |                             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 |  |  |                             continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 |  |  |                     if ($request->hasFile('main.'.$fieldName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 |  |  |                         $fileName = uniqid() . '.' . $request->file('main.'.$fieldName)->getClientOriginalExtension(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 |  |  |                         $request->file('main.'.$fieldName)->move( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  |                             $modelPath, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 411 |  |  |                             $fileName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 412 |  |  |                         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 413 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 414 |  |  |                         $requestValue = $modelFolder . $fileName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 415 |  |  |                     } elseif (! $request->file('main.'.$fieldName)->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 416 |  |  |                         throw new \Exception($request->file('main.'.$fieldName)->getErrorMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 417 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 418 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 419 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 420 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 421 | 1 |  |                 if (! $field->saveIfEmpty() && empty($requestValue)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 422 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 423 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 424 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 425 | 1 |  |                 if (! empty($requestValue)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 426 |  |  |                     $item->setAttribute( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 427 |  |  |                         $fieldName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 428 |  |  |                         $field->applyFunctions($requestValue) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 429 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 430 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 431 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 432 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 433 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 434 | 1 |  |         $item->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 435 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 436 | 1 |  |         $this->setInstance($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 437 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 438 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 439 | 1 |  |         if (! empty($relations = $this->getRelations())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 440 | 1 |  |             foreach ($relations as $relationKey => $relation) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 441 | 1 |  |                 if ($relation instanceof Collection) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 442 |  |  |                     $input = $request->input($relationKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 443 |  |  |                     $relation->get('relation')->persist($input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 444 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 445 | 1 |  |                     $relation->persist($request->input($relationKey)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 446 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 447 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 448 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 449 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 450 | 1 |  |         return $item; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 451 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 452 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 453 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 454 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 455 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 456 | 1 |  |     public function getValidationRules() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 457 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 458 | 1 |  |         return $this->generator->getValidationRules(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 459 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 460 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 461 |  |  |  | 
            
                        
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.