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 关注
- 133 浏览
添加回答
举报