Laravel: How does the query builder handle ->first()?
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)
Comments
Post a Comment