問(wèn)題描述
我不確定我是否完全理解 Laravel Eloquent 屬性轉(zhuǎn)換.根據(jù)文檔,(https://laravel.com/docs/8.x/eloquent-mutators#attribute-casting),這些是支持的類型:
I am not sure I fully understand Laravel Eloquent attribute casting. According to documentation, (https://laravel.com/docs/8.x/eloquent-mutators#attribute-casting), these are the supported types:
整數(shù)、實(shí)數(shù)、浮點(diǎn)數(shù)、雙精度數(shù)、十進(jìn)制數(shù):、字符串、布爾值、對(duì)象、數(shù)組、集合、日期、日期時(shí)間、時(shí)間戳、加密、加密:對(duì)象、加密:數(shù)組和加密:集合
integer, real, float, double, decimal:, string, boolean, object, array, collection, date, datetime, timestamp, encrypted, encrypted:object, encrypted:array, and encrypted:collection
到目前為止,我只在我的模型上使用了日期轉(zhuǎn)換(當(dāng)字段作為時(shí)間戳存儲(chǔ)在數(shù)據(jù)庫(kù)中時(shí)),如下所示:
So far, I've only used date casting on my models (when the fields were stored as timestamps in the db), like this:
protected $dates = [
'modified_at', 'published_at'
];
我也理解當(dāng)值存儲(chǔ)為整數(shù)(0 或其他)時(shí)需要將屬性轉(zhuǎn)換為布爾值.
I also understand the need for attribute casting to boolean when the values are stored as integers (0 or sth else).
但是對(duì)于其他屬性類型(例如整數(shù)),我應(yīng)該始終進(jìn)行屬性轉(zhuǎn)換嗎?或者只是當(dāng)數(shù)據(jù)庫(kù)中的字段屬于不同類型時(shí)?有哪些用例或其他屬性的最佳實(shí)踐是什么?
But what about other attribute types (integers, for example), should I always do attribute casting? Or just when the field in the database is of a different type? What are the use cases or what is the best practice with other attributes?
(例如,我無(wú)法想象在遷移中創(chuàng)建一個(gè)字符串字段,然后將其中的一些數(shù)字保存為字符串,然后將其轉(zhuǎn)換回模型上的整數(shù)?)
(I can't, for example, imagine creating a string field in migrations, then saving some number inside it as string and then casting it back into an integer on the model?)
推薦答案
默認(rèn)情況下,屬性將轉(zhuǎn)換為表中定義的列類型.因此,如果您的列是整數(shù),那么它將被轉(zhuǎn)換為 int
.
By default, attributes will be casted to the type of column defined in the table. So, if your column is an integer, then it will be casted as int
.
但是,如果您想為特定字段修改此行為,會(huì)發(fā)生什么?這就是屬性轉(zhuǎn)換進(jìn)入場(chǎng)景的時(shí)候.
But, what happens if you want to modify this behavior for specific fields? That's when attribute casting enters the scene.
例如,假設(shè)我們?cè)诿麨?projects
的表中有一個(gè)名為 config
的列,類型為 json
,我們可以在其中存儲(chǔ)其他配置元素每個(gè)項(xiàng)目.
For example, imagine we have in a table called projects
a column named config
of type json
in which we can store additional configuration elements for each project.
就我而言,能夠?qū)⑦@些數(shù)據(jù)作為 array
處理會(huì)很有用.因此,我們無(wú)需接收string
或object
,而只需處理一個(gè)簡(jiǎn)單的array
.為此,我們只需:
In my case, it'd be useful to be able to handle this data as an array
. So, instead of receiving a string
or object
, we can just deal with a simple array
. To do this, we just:
class Project extends Model
{
// ...
protected $casts = [
'config' => 'array',
];
// ...
}
這樣,每當(dāng)我們使用 Eloquent 從數(shù)據(jù)庫(kù)中獲取項(xiàng)目時(shí),每條記錄都會(huì)將該屬性作為 array
.而且,當(dāng)嘗試存儲(chǔ)/更新記錄時(shí),它會(huì)被轉(zhuǎn)換回 json
.
This way, whenever we use Eloquent to fetch projects from the database, each record will have that property as an array
. And also, it will be converted back to json
when trying to store/update records.
與您指定的情況有關(guān)(將元素保存為 string
,然后將其檢索為 integer
)當(dāng)然是完全可能的.您需要為該字段設(shè)置訪問(wèn)器和修改器.對(duì)于名為 number
的屬性:
Related to the case you specified (saving element as a string
but then retrieve it as an integer
) is totally possible of course. You'll need to set both the accessor and the mutator for that field. For an attribute named number
:
/**
* This will be called when fetching the element.
*/
public function getNumberAttribute($value)
{
return (int)$value;
}
/**
* This will be called when storing/updating the element.
*/
public function setFirstNameAttribute($value)
{
$this->attributes['number'] = (string)$value;
}
現(xiàn)在,有什么需要這樣做的理由嗎?好吧,您可能正在處理一個(gè)設(shè)計(jì)不當(dāng)?shù)臄?shù)據(jù)庫(kù),或者正在處理多個(gè)系統(tǒng)正在使用的生產(chǎn)數(shù)據(jù)庫(kù)并且數(shù)據(jù)庫(kù)很難完成.在這些情況下,您可以利用這種值操作在您的應(yīng)用中按您的意愿工作.
Now, a reason to ever need to make this? Well, you could be dealing with a database not properly well designed, or with a production database that is being used by multiple systems and changes in the db are hard to accomplish. In those cases you could make use of this kind of value manipulation to work as you want in your app.
這篇關(guān)于Laravel eloquent 模型模型屬性轉(zhuǎn)換(我應(yīng)該轉(zhuǎn)換哪些屬性類型?)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!