1 回答

TA貢獻1877條經驗 獲得超1個贊
所以我找到了解決這個問題的方法:
在需要查詢布爾值的模型中,將布爾特征的名稱添加為鍵,將“boolean”添加為值,如下所示:
protected $casts = [
'waterproof' => 'boolean'
];
然后,在控制器中:
// Get all the item's special features and "clean" them:
$specialFeaturesValues = $specialFeatures::where('gear_items_id', $gearItem->id)->get();
$specialFeaturesRejects = ['id' => 'xy', 'gear_items_id' => 'xy', 'created_at' => 'xy', 'updated_at' => 'xy'];
$specialFeaturesClean = array_diff_key($specialFeaturesValues[0]->getAttributes(), $specialFeaturesRejects);
$booleanFeatures = array_keys($specialFeaturesValues[0]->getCasts(), 'boolean');
foreach($booleanFeatures as $booleanFeature){
if ($specialFeaturesClean[$booleanFeature] > 0){
$specialFeaturesClean[$booleanFeature] = 'Yes';
} elseif($specialFeaturesClean[$booleanFeature] === 0){
$specialFeaturesClean[$booleanFeature] = 'No';
} else {
$specialFeaturesClean[$booleanFeature] = 'Unknown';
};
}
(不要忘記使用“發送”$specialFeaturesClean到視圖compact())
在刀片視圖中,這個簡單的foreach循環將顯示“干凈”值,將“1”(或任何其他真實值)替換為“是”,將“0”替換為“否”,將“空”替換為“未知”。(例如見圖片)
- 1 回答
- 0 關注
- 179 瀏覽
添加回答
舉報