Freddywhest / firestore-eloquent

This Laravel library provides an Eloquent-like interface for interacting with Firestore, Google Cloud's NoSQL database.
https://firestore-eloquent.netlify.app
MIT License
5 stars 3 forks source link

Returns an empty object when retrieving from firestore #7

Closed alsong closed 3 months ago

alsong commented 3 months ago

Describe the bug A clear and concise description of what the bug is. I am using an already existing firestore database. I created a document and it appeared in the collection. Now when i retrieve the documents in the collection i get an array of empty objects, the fields do not show up although the number of objects is correct.

To Reproduce Steps to reproduce the behavior: I never changed the model file. I just made $users = UserRoles::all();

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem. image

Desktop (please complete the following information):

Additional context The project is on laravel 10.

Freddywhest commented 3 months ago

Hello @alsong, Can you provide the code for UserRoles model

alsong commented 3 months ago

@Freddywhest `<?php

namespace App\FModels;

use Roddy\FirestoreEloquent\Facade\FModel;

class UserRoles extends FModel { /**

alsong commented 3 months ago

although when i do $users = UserRoles::first(); echo $users returns an empty object but $users->name returns the name.

Freddywhest commented 3 months ago

dd() the users, if it also returns empty object

alsong commented 3 months ago

when you dd($users); its not empty. It actually shows logs image_2024-03-12_132848622

which exists in the database image

alsong commented 3 months ago

@Freddywhest i think i have found a work by for now.

$users = UserRoles::first(); return response()->json(['message' => 'Test complete', 'data' => $users->data()], 200);

so we add data() at the end and then it works

Freddywhest commented 3 months ago

The reason why you got an empty objects when using postman is because UserRoles::all(); returns a php class but not an array that's why

Freddywhest commented 3 months ago

@Freddywhest i think i have found a work by for now.

$users = UserRoles::first(); return response()->json(['message' => 'Test complete', 'data' => $users->data()], 200);

so we add data() at the end and then it works

Oh that's great, I will add it to the documentation. Thanks

alsong commented 3 months ago

but now how do i resolve UserRoles::all(); with postman because it will be used by many

Freddywhest commented 3 months ago
$roles = UserRoles::all();
    $array = [];
    foreach($roles as $r){
        array_push($array, $r->data());
    }
    return response()->json(['message' => 'Test complete', 'data' => $array], 200);

For the meantime, feel free to make use of this. I'll be integrating support for array return in the API (Postman) with the next update.

Freddywhest commented 3 months ago

Let me know me it works

alsong commented 3 months ago

Yes it works ofcourse. Btw it will be showing some support for this project.

alsong commented 3 months ago

But please don't do this $users->data() it takes away the whole feel of using eloquent.

Freddywhest commented 3 months ago

But please don't do this $users->data() it takes away the whole feel of using eloquent.

That's true, ->data() is for API use only. When using it with Laravel there's no need to use ->data()

alsong commented 3 months ago

But please don't do this $users->data() it takes away the whole feel of using eloquent.

That's true, ->data() is for API use only. When using it with Laravel there's no need to use ->data()

so me using inertiajs will not disturb me

Freddywhest commented 3 months ago

I have tried it with livewire but not with inertiajs. I think inertia should be something like

$users = User::first()
return Inertia::render('Users', [
            'user' => response()->json($users->data())
        ]);

OR

$users = Users::all();
    $array = [];
    foreach($users as $u){
        array_push($array, $u->data());
    }

    return Inertia::render('Users', [
            'users' => response()->json($array)
        ]);
Freddywhest commented 3 months ago

Also try something like this and let me know if it works

$user = User::first()
return Inertia::render('Users', [
            'user' => $user
        ]);

OR

$users = Users::all();

    return Inertia::render('Users', [
            'users' => $users
        ]);
alsong commented 3 months ago

this is funny but this is how pagination would work

$roles = UserRoles::paginate(10);
$array = [];
foreach($roles->data() as $r){
    array_push($array, $r->data());
}
return response()->json(['message' => 'Test complete', 'data' => $array], 200);
Freddywhest commented 3 months ago

this is funny but this is how pagination would work $roles = UserRoles::paginate(10); $array = []; foreach($roles->data() as $r){ array_push($array, $r->data()); } return response()->json(['message' => 'Test complete', 'data' => $array], 200);

That's simple, here's an example:

$roles = UserRoles::paginate(10);
$pagination = $roles->pagination(); //It will provide the pagination info.

/**
sample result for $pagination
"pagination": {
        "total_pages": 8,
        "per_page": 10,
        "current_page": 1,
        "last_page": 8,
        "next_page": 2,
        "prev_page": null,
        "from": 0,
        "to": 10,
        "total": 80
    }
*/

$array = [];
foreach($roles->data() as $r){
    array_push($array, $r->data());
}
return response()->json(['message' => 'Test complete', 'data' => $array, "pagination" => $pagination], 200); // add the pagination to the response
alsong commented 3 months ago

i think let me close the issue, you might have to update the docs.

Freddywhest commented 3 months ago

Sure, I'll include API documentation in the documentation for the upcoming update.