Laravel“查找”方法“哪里 id = ?” 给出错误的结果

use*_*021 2 postgresql laravel laravel-7

我正在使用 Laravel 7。我有名为“Stores”的表,列“id”是主键和自动增量。

\n

当我运行时Store::find(9)->first()它给出错误的结果,当我运行时Store::where('id', 9)->first()给出正确的结果。

\n

我启用了查询日志并得到了关注。

\n

为了Store::find(9)->first()

\n
array:2 [\n  0 => array:3 [\n    "query" => "select * from "stores" where "stores"."id" = ? limit 1"\n    "bindings" => array:1 [\n      0 => 9\n    ]\n    "time" => 6.41\n  ]\n  1 => array:3 [\n    "query" => "select * from "stores" limit 1"\n    "bindings" => []\n    "time" => 0.53\n  ]\n]\n
Run Code Online (Sandbox Code Playgroud)\n

并为Store::where('id', 9)->first()

\n
array:1 [\xe2\x96\xbc\n  0 => array:3 [\xe2\x96\xbc\n    "query" => "select * from "stores" where "id" = ? limit 1"\n    "bindings" => array:1 [\xe2\x96\xbc\n      0 => 9\n    ]\n    "time" => 6.89\n  ]\n]\n
Run Code Online (Sandbox Code Playgroud)\n

小智 5

你不需要打电话first()find(id)

https://laravel.com/docs/9.x/eloquent#retriving-single-models

//Both results will be the same

Store::find(9);

Store::where('id', 9)->first();
Run Code Online (Sandbox Code Playgroud)