Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel检查集合是否为空?_Php_Laravel_Eloquent - Fatal编程技术网

Php 如何在Laravel检查集合是否为空?

Php 如何在Laravel检查集合是否为空?,php,laravel,eloquent,Php,Laravel,Eloquent,我正在检查一封来自DB的电子邮件。该电子邮件不存在。但是,如何检查集合中没有匹配行?我正在使用isEmpty()方法,但它不起作用。我不知道为什么会这样。以下是我的代码: $conflictUser = User::where('email',$newEmail)->get(); if($conflictUser->isEmpty()) { abort(409, 'Email Adresse existiert bereits.'); } 我使用**count()==0*

我正在检查一封来自DB的电子邮件。该电子邮件不存在。但是,如何检查集合中没有匹配行?我正在使用
isEmpty()
方法,但它不起作用。我不知道为什么会这样。以下是我的代码:

$conflictUser = User::where('email',$newEmail)->get();

if($conflictUser->isEmpty()) {
    abort(409, 'Email Adresse existiert bereits.');
}
我使用
**count()==0**
实现了目标。但是为什么
isEmpty()
不起作用,这是我的问题。

首先,在哪里:

$conflictUser = User::firstWhere(['email' =>  $newEmail ]) !== null;
第一:

$conflictUser = $items->first(function($item) use($newEmail) { 
      return  $item->email === $newEmail;
}) !== null;`
过滤器:

$conflictUser = $items->filter(function($item) use($conflictUser) { 
    return $item->emp_id === $needle && $item->dst_id === $needle;
})->count() > 0;
我遇到了这个问题,它帮助我解决了这个问题,我希望它能帮助我解决这个问题

$conflictUser = User::firstWhere(['email' =>  $newEmail ]) !== null;
第一:

$conflictUser = $items->first(function($item) use($newEmail) { 
      return  $item->email === $newEmail;
}) !== null;`
过滤器:

$conflictUser = $items->filter(function($item) use($conflictUser) { 
    return $item->emp_id === $needle && $item->dst_id === $needle;
})->count() > 0;

我遇到了这个问题,它帮助我解决了这个问题,我希望它能帮助在空集合对象上返回false的sta。我认为你最初的评论是正确的<代码>$conflictUser->isEmpty(),这是一个逻辑错误。否则,发布的代码可能与问题不匹配。如果集合“为空”,则它找不到问题record@sta在空集合对象上返回false。我认为你最初的评论是正确的<代码>$conflictUser->isEmpty(),这是一个逻辑错误。否则,发布的代码可能与问题不匹配。如果集合“为空”,则未找到记录