Any way to modify charts to only return a subset of data?

I'm attempting to create a user activity chart and presently have users organized into smaller sub-sets. I wondered if there was a way to only show chart results for users within the same user's sub-set list? For example
... // the widget
[$users, $months] = $this->getPerMonth();
... // rest of widget


private function getPerMonth(): array
{
$activity = UserActivity::get()->where('organizations_id', auth()->user()->organizations_id); // not in use currently
$data = Trend::model(UserActivity::class)
->between(
start: now()->startOfYear(),
end: now()->endOfYear(),
)
->perMonth()
->count();

return [
$data->map(fn (TrendValue $value) => $value->aggregate),
$data->map(fn (TrendValue $value) => now()->rawParse($value->date)->format('M')),
];
}
... // the widget
[$users, $months] = $this->getPerMonth();
... // rest of widget


private function getPerMonth(): array
{
$activity = UserActivity::get()->where('organizations_id', auth()->user()->organizations_id); // not in use currently
$data = Trend::model(UserActivity::class)
->between(
start: now()->startOfYear(),
end: now()->endOfYear(),
)
->perMonth()
->count();

return [
$data->map(fn (TrendValue $value) => $value->aggregate),
$data->map(fn (TrendValue $value) => now()->rawParse($value->date)->format('M')),
];
}
I'm wondering if there is a way to use the $activity variable somehow instead of the class? Or can I filter it after the values are returned from getPerMonth? Thanks in advance!
Solution:
You just used the get() in the wrong order.
Jump to solution
12 Replies
Steve_OH
Steve_OHOP7mo ago
Bump
awcodes
awcodes7mo ago
You should be able to use Trend::query() instead of the model class.
Steve_OH
Steve_OHOP7mo ago
Thank you for responding! Attempted a few different variations with Trend::query(), but seem to get one of two errors. For the first query, which attempts to place $activity into the query
$data = Trend::query($activity)
$data = Trend::query($activity)
The error is (ignore the line number, I have each attempt commented/uncommented on different lines):
Flowframe\Trend\Trend::query(): Argument #1 ($builder) must be of type Illuminate\Database\Eloquent\Builder, Illuminate\Database\Eloquent\Collection given, called in E:\vsprojects\filament\app\Filament\Resources\Widgets\TrackUserLoginActivity.php on line 76
Flowframe\Trend\Trend::query(): Argument #1 ($builder) must be of type Illuminate\Database\Eloquent\Builder, Illuminate\Database\Eloquent\Collection given, called in E:\vsprojects\filament\app\Filament\Resources\Widgets\TrackUserLoginActivity.php on line 76
and when my query is:
$data = Trend::query(UserActivity::get()->where('organizations_id', auth()->user()->organizations_id))
$data = Trend::query(UserActivity::get()->where('organizations_id', auth()->user()->organizations_id))
The error returns as:
Flowframe\Trend\Trend::query(): Argument #1 ($builder) must be of type Illuminate\Database\Eloquent\Builder, Illuminate\Database\Eloquent\Collection given, called in E:\vsprojects\filament\app\Filament\Resources\Widgets\TrackUserLoginActivity.php on line 77
Flowframe\Trend\Trend::query(): Argument #1 ($builder) must be of type Illuminate\Database\Eloquent\Builder, Illuminate\Database\Eloquent\Collection given, called in E:\vsprojects\filament\app\Filament\Resources\Widgets\TrackUserLoginActivity.php on line 77
Dennis Koch
Dennis Koch7mo ago
It doesn’t work because it needs a Builder instance (before you call ::get(). BTW you code loads all activities and filters them in PHP instead of on a DB level.
Steve_OH
Steve_OHOP7mo ago
I wanted to understand what was possible programmatically If you were to simplify it on a DB level, how would you? Using a DB:: call?
Dennis Koch
Dennis Koch7mo ago
What are you trying to simplify? Can't you just apply a where condition?
Steve_OH
Steve_OHOP7mo ago
You will have to forgive me, I only recently picked up and fell in love with laravel/filament so my understanding comes from my years of PHP knowhow and from other languages I know. I guess the issue is that I'm trying to learn the type of data that should be returned in order to structure the query correctly. What I'm trying to do is to filter the Trend::model(UserActivity::class) to only output when a certain field in that class matches, eg: where('organizations_id', auth()->user()->organizations_id). The where condition seems to return the errors as shown above, whether called directly or as a variable passed in. Is it possible to utilize something like Trend(DB::table('user_activity')->where('value', $value)->get())?
awcodes
awcodes7mo ago
Trend::query(UserActivity::where(‘organizations_id’, auth()->user()->organizations_id)->get())->perMonth()->count()
Trend::query(UserActivity::where(‘organizations_id’, auth()->user()->organizations_id)->get())->perMonth()->count()
Solution
awcodes
awcodes7mo ago
You just used the get() in the wrong order.
Dennis Koch
Dennis Koch7mo ago
I think you shouldn't have a get at all. It expects a Builder
awcodes
awcodes7mo ago
Possibly, I’m just quoting my understanding from the packages readme. But yea with a Builder the get() is implied. So, you’re right.
Steve_OH
Steve_OHOP6mo ago
Solved, thank you very much!
Want results from more Discord servers?
Add your server