Laravel 中的计数函数机制

假设我们有这个代码


count() 函数的结果是关于每个查询还是在查询执行之后 count 函数返回结果?


$model=Model::query()


foreach ($Entities as $entity) {

               $model->orWhereBetween("id",$entity->first_id,$entity->last_id])

         }

$model->count()


牧羊人nacy
浏览 180回答 2
2回答

一只萌萌小番薯

Salam,答案:这取决于您何时调用 count 方法!在您的代码中:$model=Model::query();$model2 = $model;foreach ($Entities as $entity) {//you should save your where between in self $model varriable    $model = $model->orWhereBetween("id",$entity->first_id,$entity->last_id])}//$model - object of query builder$model->count();//After operations and filtering $model2->count();//Before operations and filtering (  It is equal or more than $model->count()  )我希望这可以帮助你 :)

波斯汪

$model=Model::query()    foreach ($Entities as $entity) {        $model->orWhereBetween("id",$entity->first_id,$entity->last_id])    }//$model - object of query builder$model->count();它将生成一个查询并在 db 端进行计算: SELECT COUNT(*) FROM table WHERE id BETWEEN ? AND ? OR id BETWEEN ? AND ? OR id BETWEEN ? AND ?
打开App,查看更多内容
随时随地看视频慕课网APP