What seems to be the problem:
So I have a page of users with their user information, and the user table is linked with user logs which is a separate database table, but shows the data here on the same page. The relationship between users and logs is one to many. I need to create a chart for individual users of their conversions, which is an attribute in the user log. I am unable to pass the individual user id to the controller for it to render the individual user chart.
Steps to Reproduce:
I have user laravel charts and chartjs to try to achieve this.
Are you using the latest version of Livewire:
yes
Do you have any screenshots or code examples:
public $userID;
public function render()
{
$dates= array();
$count= array();
$users = User::query()
->orderBy('name', 'asc')
->paginate(10);
$userConversion = Log::select(array(
DB::raw('DATE(`time`) as `date`'),
DB::raw('COUNT(*) as `count`')
))
->where('user_id', $this->userID)
->where('type', 'conversion')
->groupBy('date')
->get()
->toArray();
// echo '<pre>';
// print_r($userConversion);
// exit;
foreach($userConversion as $user){
array_push($dates, $user['date']);
array_push($count, $user['count']);
}
// dd($count);
$chart = new UserChart;
$chart->labels($dates);
$chart->dataset('conversion', 'line', $count);
// dd($userConversion);
return view('livewire.users.users', compact('users', 'chart'))
->extends('layouts.master ')
->section('content');
}
@foreach ($users as $user)
<div class="col-md-4">
<div class="card">
<div class="card-body">
<div class="row">
<div class="pl-5 col-md-4">
<img style="height: 100px;" src="{{asset('img/user/user.png')}}" alt="">
</div>
<div class="mt-4 ml-0 col-md-8">
<div class="row">
<h3 class="card-title">{{$user->name}}</h3>
</div>
<div class="row">
<h6 class="mb-2 card-subtitle text-muted">{{$user->occupation}}</h6>
</div>
</div>
</div>
<div class="row">
<div class="col-md-7">
{!! $chart->container() !!}
</div>
<div class="col-md-5">
<p class="mb-0">{{$user->userLogs->where('type', 'impression')->count('type')}}</p>
<p class="mt-0 text-muted">Impressions</p>
<p class="mt-3 mb-0">{{$user->userLogs->where('type', 'conversion')->count('type')}}</p>
<p class="mt-0 text-muted">Conversions</p>
<p class="mt-3 mb-0">{{$user->userLogs->sum('revenue')}}</p>
<p class="mt-0 text-muted">Revenue</p>
</div>
</div>
</div>
</div>
</div>
@endforeach