Closed alsong closed 3 months ago
Hello @alsong, Can you provide the code for UserRoles model
@Freddywhest `<?php
namespace App\FModels;
use Roddy\FirestoreEloquent\Facade\FModel;
class UserRoles extends FModel { /**
Name of your firestore collection */ protected $collection = 'customersData';
/**
The primary key of the model/collection */ protected $primaryKey = 'id';
/**
Fillable property should ba an array. e.g ['id', 'age', 'name'] */ protected $fillable = [];
/**
Required property should ba an array. e.g ['id', 'name'] */ protected $required = [];
/**
Default property should ba an array. e.g ['name' => 'Alfred', 'date' => date('Y-m-d)] */ protected $default = [];
/**
although when i do $users = UserRoles::first(); echo $users returns an empty object but $users->name returns the name.
dd() the users, if it also returns empty object
when you dd($users); its not empty. It actually shows logs
which exists in the database
@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
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 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
but now how do i resolve UserRoles::all();
with postman because it will be used by many
$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.
Let me know me it works
Yes it works ofcourse. Btw it will be showing some support for this project.
But please don't do this $users->data()
it takes away the whole feel of using eloquent.
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()
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
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)
]);
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
]);
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);
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
i think let me close the issue, you might have to update the docs.
Sure, I'll include API documentation in the documentation for the upcoming update.
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](https://github.com/Freddywhest/firestore-eloquent/assets/19310511/daef3c86-1f1c-4351-bca8-08b8438ef802)
Desktop (please complete the following information):
Additional context The project is on laravel 10.