So, I have tried getting help on this problem before, and have a good start, but i am having difficulties figuring out how to implement the method to get my required data. I put it off for a while cause it was taking too long to figure out, but now I am trying again. I'm not sure if it should be called in blade, or after the model query, or what.
I am simply trying to parse a users list, with related models and count gender and age groups per model.
In this instance. People who like a hobby. But I'd like to make this a trait that I can use elsewhere to get demographics for people who like a group, live in certain areas, etc.
This is in my Demographics trait:
trait Demographics
{
public $itemStats = [];
public $userCounts = 0;
public $genders = ['male', 'female', 'other'];
public $ages = ['minors' => [0, 17],
'twenties' => [18, 29],
'thirties' => [30, 39],
'forties' => [40, 49],
'fifties' => [50, 59],
'sixties' => [60, 200],];
public function getAges ()
{
foreach ($this->ages as $ageKey => [$min, $max]) {
$ageCount = $data->users->whereBetween('age', [$min, $max])->count();
$itemStats[$ageKey . '_count'] = $ageCount;
$itemStats[$ageKey . '_percent'] = round(($ageCount / $userCount) * 100);
}
}
public function getGenders ()
{
foreach ($this->genders as $gender) {
$genderCount = $data->users->where('gender', $gender)->count();
$itemStats[$gender . '_count'] = $genderCount;
$itemStats[$gender . '_percent'] = round(($genderCount / $userCount) * 100);
}
}
I think my main point of confusion is do I reference getGenders/ Ages, or $itemStats.
In the Hobbies livewire component I have
public function getHobbiesQueryProperty ()
{
$status = $this->selectedStatus;
$cats = $this->selectedCategory;
$hobbies = Hobby::with('equipment', 'categories', 'hobbyAltNames', 'hobbyType')
->withCount('users')
->when($this->search != '', function ($q) {
$q->search(trim($this->search));
})
->when($status != '', function ($q) use ($status) {
$q->status($status);
})
->when($cats != '', function ($q) use ($cats) {
$q->filterByCategory($cats);
});
...MORE SORTING CODE
}
public function getHobbiesProperty ()
{
return $this->hobbiesQuery->paginate($this->paginate);
}
and in render I have
$data = $this->hobbies;
Then in my blade I have a tab divided list of my hobbies. Meaning there is a lot of data displayed for each hobby, so one tab may have categories and other data displayed, another tab has counts of related objects, and my demographics tab will simply display demographics when i get it working. The data itself is paginated. All the other tabs display the appropriate info, but I cannot get the demographics data displayed.
@foreach($data as $hobby)
<tr>
<td></td>
<td><a href="{{route('hobbies.show', $hobby->id)}}">{{$hobby->hobby_name}}</a></td>
<td>{{$hobby->users_count}}</td>
<td>{{$hobby->male_percent}}</td>
<td>{{$hobby->female_percent}}</td>
<td>{{$hobby->other_percent}}</td>
<td>{{$hobby->minors_percent}}</td>
<td>{{$hobby->twenties_percent}}</td>
<td>{{$hobby->thirties_percent}}</td>
<td>{{$hobby->fourties_percent}}</td>
<td>{{$hobby->fifties_percent}}</td>
<td>{{$hobby->sixties_percent}}</td>
@endforeach
So again my problem is what and where to reference the created itemstats from the demographics trait. And users count has to be determined for each related model for the calculation. So somehow I need to take the value created by " ->withCount('users')" in the hobbies query and use it for the getAges and get genders methods. I had a getStats function somewhere as well, that I guess would reference the previous two.
I'm not sure if having the itemstats is necessarily appropriate here as I think that would require a separate loop if I am not mistaken. But I need the stats to be included in the $data variable along with the other data.