我的代码如下;
$stock_property_templates = StockPropertyTemplate::where('group_id' , 100)->where('type' , 'property')->get();
始终输出以下代码:此处
if($stock_property_templates) {
echo "have Data";
}else {
echo "do not Have Data";
}
此代码正在运行,但我不希望它以这种方式工作 - > " []"
if($stock_property_templates != "[]") {
echo "have Data";
return $stock_property_templates;
}else {
echo "do not Have Data";
}
如何编写健康的代码?
答案 0 :(得分:2)
使用isNotEmpty()
方法检查集合是否为空:
if ($stock_property_templates->isNotEmpty())
或count()
:
if (count($stock_property_templates))
或者:
if ($stock_property_templates->count())
答案 1 :(得分:1)
您可以通过2种方式查看
方式
$stock_property_query = StockPropertyTemplate::where('group_id' , 100)->where('type' , 'property');
$stock_property_result = $stock_property_query->get();
if( $stock_property_query->count() == 0 ) {
echo "do not Have Data";
} else {
echo "Have Data";
}
方式二
$stock_property_result = StockPropertyTemplate::where('group_id' , 100)->where('type' , 'property')->get();
if(count($stock_property_result) == 0) {
echo "do not Have Data";
} else {
echo "Have Data";
}
答案 2 :(得分:0)
检查数组empty()
if(!empty($stock_property_templates)) {
echo "girdi";
return $stock_property_templates;
}else {
return "girmedi";
}