CristalTeam / php-api-wrapper

:rainbow: Work with APIs like with Laravel Eloquent or Doctrine (no longer a dream)
MIT License
117 stars 32 forks source link

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

Open markcameron opened 2 years ago

markcameron commented 2 years ago

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 2 years ago

I came across this behaviour today and found it odd. Thanks for calling attention to it.