dongnao3990 2015-06-18 06: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 06: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)

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部