laravel中empty(),is_null() 以及isEmpty()

时间:2023-03-09 06:01:47
laravel中empty(),is_null() 以及isEmpty()

laravel中empty(),is_null() 以及isEmpty()

PHP中

empty()

empty() 函数用于检查一个变量是否为空。

if(empty($result->order)){
//操作
}

is_null()

is_null() 函数用于检测变量是否为 NULL。

     $code = Input::get('code');
if (!is_null($code)) {
return Response::json(['status' => 1]);
} else {
return Response::json(['status' => 0]);
}
//first也是
$select = Location::select('…')->where(…)->first()
if(is_null($select)){
//操作
}else{
//操作
}

laravel 中

isEmpty()

在使用 Laravel Eloquent 模型时,我们要判断取出的结果集是否为空时,以上两种方法无效,但是laravel提供了isEmpty()给我们

$select = Location::select('…')->where(…)->get()
//判断查询结果是否为空
if(isEmpty($select)){
//或if(empty($select)){
//或if($select->isEmpty)){
//操作
}else{
//操作
}