Laravel Eloquent 判断取出的结果集是否为空
2024-10-11 18:19:58
在使用Laravel Eloquent
模型时,我们可能要判断取出的结果集是否为空,但我们发现直接使用is_null
或empty
是无法判段它结果集是否为空的。
var_dump之后我们很容易发现,即使取到的空结果集, Eloquent
仍然会返回Illuminate\Database\Eloquent\Collection
对象实例。
其实,Eloquent
已经给我们封装几个判断方法。
$result = Model::where(...)->get();
//不为空则
if ($result->first()) { }
if (!$result->isEmpty()) { }
if ($result->count()) { }
参考网站:http://stackoverflow.com/questions/20563166/eloquent-collection-counting-and-detect-empty
最新文章
- shiro 实现单用户登录,一个用户同一时刻只能在一个地方登录
- 用css3实现各种图标效果(2)
- git alias和gitconfig配置
- 通俗易懂------this指向
- 「Ruby &;&; Sqlite3」How to install sqlite3 for ruby? (solve: sqlite-ruby no such file...)
- linux shell 中的位置变量
- java-脚本-编译-注解
- 使用Rails 4.2+ 测试异步邮件系统
- oracle学习 三(持续更新中)
- Longest Consecutive Sequence——Leetcode
- 关于Linux的缓存内存 Cache Memory详解<;转>;
- ueditor插件 -- 插入填空题
- 2013第四届蓝桥杯决赛Java高职高专组题目以及解法答案
- Linux下高并发socket最大连接数
- 【博客开篇】服务器配置:Windows2008R2+PHP5.6+SQLServer2008(X64)
- 第四章 HTML5概述
- centos7上编译安装mysql5.6
- 【WPF】两则动画效果
- 探讨android更新UI的几种方法
- eclipse+gnuarm+使用报错
热门文章
- opencv学习笔记(五)镜像对称
- ping 有端口的ip
- git linux
- 二、JavaScript语言--JS基础--JavaScript进阶篇--函数
- 错误解决error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file
- 版本引发的血案check the manual that corresponds to your MySQL server version for the right syntax
- 【转】reduce端缓存数据过多出现FGC,导致reduce生成的数据无法写到hdfs
- js:语言精髓笔记8--对象系统
- 水题 Codeforces Round #300 A Cutting Banner
- 在visual studio中使用git版本系统(zz)