88use Illuminate \Support \Traits \Macroable ;
99
1010/**
11- * @property array $columns
11+ * @property array $mapAttributeToColumn
1212 */
1313trait HasMappableColumns
1414{
@@ -21,13 +21,13 @@ protected static function bootHasMappableColumns(): void
2121 {
2222 static ::addGlobalScope (new MappedColumnsScope ());
2323
24- foreach ((new static )->columns ?? [] as $ mappedCol => $ dbCol ) {
25- static ::macro ('get ' . Str::studly ($ mappedCol ) . 'Attribute ' , function () use ($ dbCol ) {
26- return $ this ->$ dbCol ;
24+ foreach ((new static )->mapAttributeToColumn ?? [] as $ attribute => $ column ) {
25+ static ::macro ('get ' . Str::studly ($ attribute ) . 'Attribute ' , function () use ($ column ) {
26+ return $ this ->$ column ;
2727 });
2828
29- static ::macro ('set ' . Str::studly ($ mappedCol ) . 'Attribute ' , function ($ value ) use ($ dbCol ) {
30- $ this ->$ dbCol = $ value ;
29+ static ::macro ('set ' . Str::studly ($ attribute ) . 'Attribute ' , function ($ value ) use ($ column ) {
30+ $ this ->$ column = $ value ;
3131 });
3232 }
3333 }
@@ -37,16 +37,16 @@ public function newInstance($attributes = [], $exists = false): Model
3737 /** @var Model $instance */
3838 $ instance = parent ::newInstance ($ attributes , $ exists );
3939
40- foreach ($ instance ->columns ?? [] as $ mappedCol => $ dbCol ) {
41- $ instance ->hidden [] = $ dbCol ;
42- $ instance ->appends [] = $ mappedCol ;
40+ foreach ($ instance ->mapAttributeToColumn ?? [] as $ attribute => $ column ) {
41+ $ instance ->hidden [] = $ column ;
42+ $ instance ->appends [] = $ attribute ;
4343
44- if (in_array ($ mappedCol , $ instance ->guarded )) {
45- $ instance ->guarded [] = $ dbCol ;
44+ if (in_array ($ attribute , $ instance ->guarded )) {
45+ $ instance ->guarded [] = $ column ;
4646 }
4747
48- if (in_array ($ mappedCol , $ instance ->fillable )) {
49- $ instance ->fillable [] = $ dbCol ;
48+ if (in_array ($ attribute , $ instance ->fillable )) {
49+ $ instance ->fillable [] = $ column ;
5050 }
5151 }
5252
0 commit comments