如何查询 Laravel 7 中的列类型并检索布尔值?
How to query column type in Laravel 7 and retrieve boolean values?
有没有办法能够判断给定数据库中的某个列 table 是“布尔”类型,如果是 - 将“0”或“1”转换为“否” ”和“是”?
我有一个名为“gearitems”的 table,它包含有关登山装备的基本一般信息(如价格、重量、描述等),然后我有将近 60 个其他“一对一”关系 tables(例如:“背包”、“睡袋”、“帐篷”)根据装备的类型都有特定的“特殊功能”。例如,如果是背包,它的容量以升为单位,但如果是帐篷,则需要的木桩数量等。
我遇到的问题是,许多这些小的“特殊功能”table 具有名称不同的布尔类型列(例如“背包”中的 'waterproof' 或 'attachable'在“睡袋”中)并且当我想向用户展示一个特定的装备时,我有一个功能可以将一般的“gearitems”table加入相应的特定装备类型table。
对于所有列类型,在将数据插入数据库时显示数据没有问题,但布尔值returns“1”或“0”...我不能只转换所有“ 1”和“0”变成“是”或“否”,因为其他值可能是 1 或 0,它们不是布尔值。所以我想,如果我可以检查列类型是否是布尔类型数据,我就可以安全地将值转换为是或否,但是我该怎么做而不专门针对列的名称? (因为所有布尔类型列的列表太长,以后很难编辑)
我创建了一个名为“specialfeatures”的table,其中包含所有特殊功能名称的列表(如“waterproof”、“stakes_required”、“capacity_l”等)在每种装备类型(“背包”、sllepingbags 等)的每个 table 中。我考虑在 special_features_name 列旁边添加一个名为“boolean”的列以指示该功能是布尔值还是不是,但这似乎是一种非常粗糙和不优雅的方法。当然有更好的方法吗?
如果有帮助,以下是我的代码中的相关部分:
我的控制器:
public function show(Manufacturer $manufacturer, GearItem $gearItem, GearCategory $gearCategory)
{
// Get the item's special features Model's name:
$featureModel = SpecialFeature::where('sub_categories_id', $gearItem->sub_categories_id)->value('model_name');
$specialFeatures = 'App\'.$featureModel;
// Get the manufacturer's name and homepage:
$manufacturer->name = $manufacturer::where('id', $gearItem->manufacturer_id)->value('name');
$manufacturer->homepage = $manufacturer::where('id', $gearItem->manufacturer_id)->value('homepage');
// Get all the item's special features:
$gearItem->features = $specialFeatures::where('gear_items_id', $gearItem->id)->get();
// Iterator for the spacial features names:
$featureNames = SpecialFeature::where('sub_categories_id', $gearItem->sub_categories_id)->pluck('feature_name');
$featureNames->title = SpecialFeature::where('sub_categories_id', $gearItem->sub_categories_id)->pluck('feat_html');
return view('gearitem.show', compact(['gearCategory', 'manufacturer', 'gearItem', 'featureNames']));
}
我的看法:
@if($gearItem->url === null)
<strong>{{$gearItem->name}}</strong>
@else
<strong> <a target="_blank" href="{{$gearItem->url}}">{{$gearItem->name}}</a></strong>
@endif
by <strong><a target="_blank" href="{{$manufacturer->homepage}}">{{ $manufacturer->name }}</a></strong>
<div class="specialFeatures">
<div class="row">
<div class="col-6">
<table class="table">
@foreach ($featureNames->title as $title)
<tr>
<td>{!! $title !!}</td>
</tr>
@endforeach
</table>
</div>
<div class="col-6">
<table class="table">
@foreach ($featureNames as $featureName)
<tr>
<td>{{ $gearItem->features[0][$featureName]}}</td>
</tr>
@endforeach
</table>
</div>
</div>
</div>
所以我找到了解决这个问题的方法:
在需要查询布尔值的模型中,添加布尔特征的名称作为键,'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()
的视图)
在 blade 视图中,这个简单的 foreach
循环将显示“干净”值,将“1”(或任何其他真实值)替换为“是”,将“0”替换为“未知”的“否”和“空”。 (见图片示例)
希望对您有所帮助... ;-)
有没有办法能够判断给定数据库中的某个列 table 是“布尔”类型,如果是 - 将“0”或“1”转换为“否” ”和“是”?
我有一个名为“gearitems”的 table,它包含有关登山装备的基本一般信息(如价格、重量、描述等),然后我有将近 60 个其他“一对一”关系 tables(例如:“背包”、“睡袋”、“帐篷”)根据装备的类型都有特定的“特殊功能”。例如,如果是背包,它的容量以升为单位,但如果是帐篷,则需要的木桩数量等。
我遇到的问题是,许多这些小的“特殊功能”table 具有名称不同的布尔类型列(例如“背包”中的 'waterproof' 或 'attachable'在“睡袋”中)并且当我想向用户展示一个特定的装备时,我有一个功能可以将一般的“gearitems”table加入相应的特定装备类型table。
对于所有列类型,在将数据插入数据库时显示数据没有问题,但布尔值returns“1”或“0”...我不能只转换所有“ 1”和“0”变成“是”或“否”,因为其他值可能是 1 或 0,它们不是布尔值。所以我想,如果我可以检查列类型是否是布尔类型数据,我就可以安全地将值转换为是或否,但是我该怎么做而不专门针对列的名称? (因为所有布尔类型列的列表太长,以后很难编辑)
我创建了一个名为“specialfeatures”的table,其中包含所有特殊功能名称的列表(如“waterproof”、“stakes_required”、“capacity_l”等)在每种装备类型(“背包”、sllepingbags 等)的每个 table 中。我考虑在 special_features_name 列旁边添加一个名为“boolean”的列以指示该功能是布尔值还是不是,但这似乎是一种非常粗糙和不优雅的方法。当然有更好的方法吗?
如果有帮助,以下是我的代码中的相关部分:
我的控制器:
public function show(Manufacturer $manufacturer, GearItem $gearItem, GearCategory $gearCategory)
{
// Get the item's special features Model's name:
$featureModel = SpecialFeature::where('sub_categories_id', $gearItem->sub_categories_id)->value('model_name');
$specialFeatures = 'App\'.$featureModel;
// Get the manufacturer's name and homepage:
$manufacturer->name = $manufacturer::where('id', $gearItem->manufacturer_id)->value('name');
$manufacturer->homepage = $manufacturer::where('id', $gearItem->manufacturer_id)->value('homepage');
// Get all the item's special features:
$gearItem->features = $specialFeatures::where('gear_items_id', $gearItem->id)->get();
// Iterator for the spacial features names:
$featureNames = SpecialFeature::where('sub_categories_id', $gearItem->sub_categories_id)->pluck('feature_name');
$featureNames->title = SpecialFeature::where('sub_categories_id', $gearItem->sub_categories_id)->pluck('feat_html');
return view('gearitem.show', compact(['gearCategory', 'manufacturer', 'gearItem', 'featureNames']));
}
我的看法:
@if($gearItem->url === null)
<strong>{{$gearItem->name}}</strong>
@else
<strong> <a target="_blank" href="{{$gearItem->url}}">{{$gearItem->name}}</a></strong>
@endif
by <strong><a target="_blank" href="{{$manufacturer->homepage}}">{{ $manufacturer->name }}</a></strong>
<div class="specialFeatures">
<div class="row">
<div class="col-6">
<table class="table">
@foreach ($featureNames->title as $title)
<tr>
<td>{!! $title !!}</td>
</tr>
@endforeach
</table>
</div>
<div class="col-6">
<table class="table">
@foreach ($featureNames as $featureName)
<tr>
<td>{{ $gearItem->features[0][$featureName]}}</td>
</tr>
@endforeach
</table>
</div>
</div>
</div>
所以我找到了解决这个问题的方法:
在需要查询布尔值的模型中,添加布尔特征的名称作为键,'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()
的视图)
在 blade 视图中,这个简单的 foreach
循环将显示“干净”值,将“1”(或任何其他真实值)替换为“是”,将“0”替换为“未知”的“否”和“空”。 (见图片示例)
希望对您有所帮助... ;-)