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{
    //操作
}

相关文章:

  • 2022-01-27
  • 2021-07-05
  • 2021-11-08
  • 2022-12-23
  • 2021-08-22
  • 2021-12-26
  • 2021-07-18
  • 2021-11-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-26
  • 2021-06-02
  • 2021-08-09
  • 2021-11-30
  • 2021-09-09
相关资源
相似解决方案