Model::find($id) returns an empty Model as opposed to null
Opened this issue · 1 comments
markcameron commented
When running Model::find($id)
in a Laravel environment, and that the Model with the id $id
doesn't exist, the return value is an empty Model
instance, as opposed to null
when calling a real Eloquent model that doesn't find a record in the database.
I've tried returning null
, []
, false
in the Transport class when a model isn't found, but it is returning the empty model instance for all values.
I'm not sure if this is the expected behavior or not. I didn't appear to find a way of having null
instead of the empty model when looking through the code.
dd(User::find(1));
---
App\Models\User {[#1639 ▼]()
#entity: "user"
#primaryKey: "id"
+exists: true
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#casts: []
#appends: []
#dates: []
#dateFormat: "Y-m-d H:i:s"
#relations: []
#hidden: []
#visible: []
}
jorbascrumps commented
I came across this behaviour today and found it odd. Thanks for calling attention to it.