Skip to content

Model::find($id) returns an empty Model as opposed to null #33

@markcameron

Description

@markcameron

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: []
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions