在使用Laravel Eloquent
模型时,咱们可能要判断取出的结果集是否为空,但咱们发现直接使用is_null
或empty
是没法判段它结果集是否为空的。html
var_dump以后咱们很容易发现,即便取到的空结果集, Eloquent
仍然会返回Illuminate\Database\Eloquent\Collection
对象实例。
其实,Eloquent
已经给咱们封装几个判断方法。laravel
$result = Model::where(...)->get(); //不为空则 if ($result->first()) { } if (!$result->isEmpty()) { } if ($result->count()) { }
参考网站:http://stackoverflow.com/questions/20563166/eloquent-collection-counting-and-detect-emptypost
转自:http://douyasi.com/laravel/eloquent_collection_detect_empty.html网站