'Laravel "Find" method "Where id = ?" gives wrong result
I'm using Laravel 7. I have table named "Stores" and column "id" is the primary key and auto increment.
When I run Store::find(9)->first() it gives wrong result and when I run Store::where('id', 9)->first() is giving correct result.
I enable the query log and got following.
For Store::find(9)->first()
array:2 [
0 => array:3 [
"query" => "select * from "stores" where "stores"."id" = ? limit 1"
"bindings" => array:1 [
0 => 9
]
"time" => 6.41
]
1 => array:3 [
"query" => "select * from "stores" limit 1"
"bindings" => []
"time" => 0.53
]
]
and for Store::where('id', 9)->first()
array:1 [▼
0 => array:3 [▼
"query" => "select * from "stores" where "id" = ? limit 1"
"bindings" => array:1 [▼
0 => 9
]
"time" => 6.89
]
]
Solution 1:[1]
You don't need to call first() with find(id)
https://laravel.com/docs/9.x/eloquent#retrieving-single-models
//Both results will be the same
Store::find(9);
Store::where('id', 9)->first();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Noman Saleem |
