You probably need to use the toArray method.
public function set(User $user) {
$userResource = new UserResource($user);
$this->firestore->document($user->id)->set($userResource->toArray());
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I want to store data on Firestore. I need to send an array to Firestore. I have API resources and I wanted to use it.
My resource:
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'phone' => $this->phone,
'addresses' => AddressResource::collection($this->addresses),
];
}
Firestore:
public function set(User $user){
$userResource = new UserResource($user);
$this->firestore->document($user->id)->set($userResource);
}
The error:
Argument 1 passed to Google\Cloud\Firestore\DocumentReference::set() must be of the type array, object given...
This code works fine
public function set(User $user){
$this->firestore->document($user->id)->set(json_decode((new UserResource($user))->toJson(), true));
}
Please or to participate in this conversation.