dongnao3990 2015-06-18 14:33
浏览 410
已采纳

Laravel:查询构建器如何处理 - > first()?

How does the query builder handle ->first()?

Does it simply do a Limit 1 or does it do some advanced checking to make sure if only 1 row is returned?

In rare cases, the application may want to ensure that ONLY 1 row will be returned. Perhaps ->first() is not the best solution?

Any insight or direction is greatly appreciated!

  • 写回答

1条回答 默认 最新

  • dongmi5015 2015-06-18 14:44
    关注

    The first() method will always return one result or null if no results are found. Here's all it does:

    public function first($columns = array('*'))
    {
        $results = $this->take(1)->get($columns);
    
        return count($results) > 0 ? reset($results) : null;
    }
    

    The difference to just using take(1) (or limit(1)) is that it actually just returns one result object, by calling reset(). (Also, first() obviously executes the query. The others don't)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度