Laravel使用first时返回表中的第一条记录

jea*_*frg 2 database record laravel

我正在尝试获取id = n的记录,但是当我使用“-> first()”获取找到的第一条记录时,Laravel在整个表中给了我第一条记录。

在此屏幕快照中,要搜索的id是“ 28”,其余的是Laravel在使用find和首先一起使用时返回的内容的var_dump。

http://i.imgur.com/WHubKp7.png

下面的代码是整个方法的一小段代码。

public function update($id)
{
    $success = false;
    $error_code = 400;
    $object = null;
    $updated_at = null;

    if (is_numeric($id) && $id > 0) {
        echo $id;
        $category_type = category_type::find($id)->first();
        return var_dump($category_type);
Run Code Online (Sandbox Code Playgroud)

请不要在未先调用该方法的情况下按预期工作。

Joe*_*inz 5

这就是预期的行为。如果您查看源代码,则会发现此方法:

public function first($columns = array('*'))
{
    return $this->take(1)->get($columns)->first();
}
Run Code Online (Sandbox Code Playgroud)

换句话说,Laravel first()在您执行时会自动为您进行呼叫find()