【发布时间】:2018-11-25 12:24:07
【问题描述】:
您将如何更改集合的属性?
所以返回的是属性改变的模型集合,而不是数组集合。
问题是,我想做一些翻译演示者,所以当我得到集合时,我可以 dd($collection) 而不是'name',它会显示'nome',也使用 response()- >json($collection) 将显示更改后的名称。
类似 $presenterNames = ['name' => 'nome', 'id' => 'identificador'];并且不在此处的名称将正常调用。
Collection {#213 ▼
#items: array:2 [▼
0 => Category {#215 ▼
+timestamps: false
#fillable: array:2 [▶]
#connection: "mysql"
#table: "categories"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:3 [▼
"id" => 1
"name" => "categoria1"
"slug" => "categoria1"
]
#original: array:3 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▶]
#slugOptions: null
}
1 => Category {#219 ▶}
]
}
所以基本上我想改变
#attributes: array:3 [▼
"id" => 1
"name" => "categoria1"
"slug" => "categoria1"
]
到这里
#attributes: array:3 [▼
"identificador" => 1
"nome" => "categoria1"
"slug" => "categoria1"
]
使用关联数组进行动态处理。
【问题讨论】:
-
为什么要改变这个,为什么不从 db 改变?
-
因为我想使用英文约定维护数据库,但我希望能够在需要时翻译密钥,使用分形真的很容易,但分形仅适用于数组或 json响应,但如果该响应不是数组或 json,我想仍然使用该集合。
标签: laravel laravel-5.7 laravel-collection