Two related problems when working with a model instance returned from a query:
1. json_encode($model) returns {}
$user = User::where('id', 1)->first();
return ['ok' => true, 'user' => $user]; // "user": {}
Model did not implement JsonSerializable. PHP's default behavior for json_encode on an object is to serialize only public properties — and Model has none. toArray() and toJson() worked correctly, but passing a model directly to json_encode or returning it inside an array silently produced an empty object.
2. IDE loses type after where()->first()
$user = User::where('active', 1)->first();
$user-> // IDE shows no suggestions
Model::where() returned a raw Builder whose first() is typed as object|false. The IDE had no way to know the result was a User instance, so autocompletion was lost entirely.
Two related problems when working with a model instance returned from a query:
1.
json_encode($model)returns{}Modeldid not implementJsonSerializable. PHP's default behavior forjson_encodeon an object is to serialize only public properties — andModelhas none.toArray()andtoJson()worked correctly, but passing a model directly tojson_encodeor returning it inside an array silently produced an empty object.2. IDE loses type after
where()->first()Model::where()returned a rawBuilderwhosefirst()is typed asobject|false. The IDE had no way to know the result was aUserinstance, so autocompletion was lost entirely.