laravel Eloquent 查询数据库判断获取的内容是否为空

原文地址:https://www.cnblogs.com/love-snow/articles/7205338.html

在使用 Laravel Eloquent 模型时,我们要判断取出的结果集是否为空,但我们发现直接使用 is_null 或 empty是无法判段它结果集是否为空的!!!

var_dump 之后我们很容易发现,即使取到的空结果集,Eloquent 仍然会返回object(IlluminateSupportCollection)对象实例。
其实,Eloquent 已经给我们封装几个判断方法如下:

$users = DB::table('users')->where('id',$id)->get();
复制代码
1 if ($users->first()) {
2     //
3  } 
4 if (!$users->isEmpty()) {
5     //
6  } 
7 if ($users->count()) {
8     //
9  }
复制代码
原文地址:https://www.cnblogs.com/yiweiyihang/p/8482592.html