Laravel Eloquent发现返回null

Jak*_*bbs 4 php mysql laravel eloquent

我正在尝试使用Eloquents find方法从我的数据库中检索记录,但是,它意外地返回null.如果我在数据库上手动运行查询,则返回预期的记录.

我在Laravel中使用以下内容:

$support = Support::find(02155);
Run Code Online (Sandbox Code Playgroud)

以下直接在数据库上:

SELECT * FROM support WHERE id = 02155;
Run Code Online (Sandbox Code Playgroud)

主键列名为'id',类型为smallint(5),unsigned和zerofill以及自动增量集.我根据Laravel应该执行的内容,在Laravel文档上建立了上述"手动"查询.

报告没有任何错误(我可以看到),如果我将Eloquent方法改为'all',那么所有记录都会被正确返回.

Jak*_*bbs 14

这是因为PHP开头的数字被认为是八进制,如下:http://php.net/manual/en/language.types.integer.php

似乎PHP在执行MySQL查询之前将数字转换为小数,这意味着查询的编号不正确.

例如:

Support::find(02155);
Run Code Online (Sandbox Code Playgroud)

变为:

'SELECT * FROM mytable WHERE id = 1133'
Run Code Online (Sandbox Code Playgroud)

解决方案

我在使用Eloquents find方法之前使用(int)将数字转换为整数来解决这个问题.如果您将数字作为字符串(即引号)传递,它也会起作用,如下所示:

Support::find('02155');
Run Code Online (Sandbox Code Playgroud)