Skip to content

Model: json_encode($model) returns {} and IDE loses type after where()->first() #51

@benkhalife

Description

@benkhalife

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    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