Lomkit / laravel-rest-api

Generate Api in seconds
https://laravel-rest-api.lomkit.com/
MIT License
297 stars 18 forks source link

statsable #109

Closed ThanhSonITNIC closed 4 months ago

ThanhSonITNIC commented 4 months ago

When we want to extend filters and interactions in resource with stats data

PostResource

public function stats(\Illuminate\Contracts\Database\Eloquent\Builder $query): array
{
    return [
        'posts_has_approved_comments' => $query->whereHas('comments', fn($q) => $q->where('approved', true))->count(),
        'posts_has_worse_comments' => $query->whereHas('comments', fn($q) => $q->where('star', '<', 3))->count(),
        'published' => $query->where('published', true)->count(),
    ];
}

Request [POST] api/posts/search

{
  "search": {
    "stats": true,
    ...
  }
}

Response

{
    "current_page": 1,
    "data": [...],
    "from": 1,
    "last_page": 1,
    "per_page": 50,
    "to": 10,
    "total": 10,
    "meta": {
        "stats": {
            "posts_has_approved_comments": 5,
            "posts_has_worse_comments": 2,
            "published": 9,
        }
    }
}
ThanhSonITNIC commented 4 months ago

https://github.com/Lomkit/laravel-rest-api/issues/105 aggregates can't be complex and multiple response keys in a relation

GautierDele commented 4 months ago

Could you please add unit tests, i'm quite sur when you'll have problems with your example because the query is manipulated multiple time and is immutable

ThanhSonITNIC commented 4 months ago

Another example, we need stats of orders:

I have some ways:

ThanhSonITNIC commented 4 months ago

Could you please add unit tests, i'm quite sur when you'll have problems with your example because the query is manipulated multiple time and is immutable

unit test better by your

ThanhSonITNIC commented 4 months ago

@GautierDele please review, it take 3 days

GautierDele commented 4 months ago

I gave you the problem, i'm not able to handle this for now