Flowframe / laravel-trend

Generate trends for your models. Easily generate charts or reports.
MIT License
720 stars 74 forks source link

Using models that have eager loading by default and/or scopes #44

Closed kurucu closed 10 months ago

kurucu commented 11 months ago

I have a model that uses eager loading by default, which seems to trip Laravel Trend over, as Laravel injects a sub query into the model query when doing so.

The following should fix it:

$query->withoutEagerLoads()

There is the same problem with models that have scopes added, but these are more easily solved:

$query->withoutGlobalScopes()
kurucu commented 10 months ago

Well I've not been paying attention, you can do this. My way was as follows:

$data = Trend::query(
    Model::query()
        ->withoutEagerLoads()
        ->withoutGlobalScopes()
)
    ->between(
        start: now()->subYear(),
        end: now(),
    )
    ->perMonth()
    ->count()
    ->toArray();