我有一個包含多個列的資料庫表,其中的列具有不同的資料型別,其中一列currency以integer.
我有一個 API 用于從資料庫中檢索所有值并顯示在 UI 中,在我的 UI 級別它們接受currency值作為字串,有沒有辦法在string不改變當前結構的情況下將貨幣值作為 a 發送資料庫。,請給我一些想法如何做到這一點..
uj5u.com熱心網友回復:
有多種方法。一種是使用屬性轉換
class User extends Model
{
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'currency' => 'string',
];
}
另一個激進的解決方案是洗掉模塊 mysqlnd,以便所有內容都以字串形式回傳。
uj5u.com熱心網友回復:
你可以只使用strval()PHP的功能
$currency = SomeModel::all()->map(function ($someRow) {
return $someRow->currrency = strval($someRow->currency);
}
順便說一句,正確的方法是使用訪問器
public function getCurrency() {
return strval($this->currency);
}
現在,每次從模型中獲取貨幣時,都會得到字串轉換值。
echo $model->currency;
// "10"
有關更多資訊,請參閱檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/405629.html
標籤:
