Reputation: 713
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
]
]
Upvotes: 2
Views: 2891
Reputation: 506
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();
Upvotes: 6