laravel-collection

Eloquent Collection: Counting and Detect Empty

怎甘沉沦 提交于 2019-11-26 04:29:32
问题 This may be a trivial question but I am wondering if Laravel recommends a certain way to check whether an Eloquent collection returned from $result = Model::where(...)->get() is empty, as well as counting the number of elements. We are currently using !$result to detect empty result, is that sufficient? As for count($result) , does it actually cover all cases, including empty result? 回答1: When using ->get() you cannot simply use any of the below: if (empty($result)) { } if (!$result) { } if (