I have a table like this Activites Table:
and i extract the data from the database to the table with this code in Controller :
$TableB1 = \DB::table('users')
->join('group_user', 'users.id', '=', 'group_user.user_id')
->join('groups', 'groups.id', '=', 'group_user.group_id')
->select(
'users.name as name',
'group_user.user_id as id',
'groups.name as groupname'
)
->get();
$view->with('TableB1',$TableB1);
return $view;
In index.blade.php
<thead>
<tr>
<th>Team</th>
<th>Name</th>
<th>No. of Showing</th>
<th>No. of Follow up</th>
<th>New Lead</th>
<th>Personal Lead</th>
</tr>
</thead>
<tbody>
<?php
use Carbon\Carbon;
foreach ($TableB1 as $data){
echo
'<tr>
<th scope="row">'. $data->groupname .'</th>
<th scope="row">'. $data->name .'</th>';
// To extract the number of meetings done by each agent(owned_by_id) everyday
$meetings = \DB::table('meetings')
->where('company_id', 1)->where('owned_by_id', $data->id)
->where(DB::raw('DAY(created_at)'), Carbon::today()->day);
echo '<th scope="row">' . $meetings->count() . '</th>';
// To extract the number of calls done by each agent(owned_by_id) everyday
$calls = \DB::table('calls')
->where('company_id', 1)->where('owned_by_id', $data->id)
->where(DB::raw('DAY(created_at)'), Carbon::today()->day);
echo '<th scope="row">' . $calls->count() . '</th>';
// To extract the number of leads created by each agent(owned_by_id) everyday
$leads = \DB::table('leads')
->where('lead_status_id', 1)->where('company_id', 1)->where('owned_by_id', $data->id)
->where(DB::raw('DAY(created_at)'), Carbon::today()->day);
echo '<th scope="row">' . $leads->count() . '</th>';
}
?>
</tr>
</tbody>
How to move the code in the php blade to the controller?
the code works but I think the code should be in the controller right ?
and if possible is there is a way to filter this data in the table by Month & year through AJAX so i don't have to refresh the page ?
I'm using Laravel 5.7
In your controller, you could do something like this:
// $TableB1 = ...
// note the & in &$entry.
// The & makes $entry writable in the loop (to say it simply)
foreach ($TableB1 as &$entry){
$entry->meetingCount = (
\DB::table('meetings')
->where('company_id', 1)->where('owned_by_id', $data->id)
->where(DB::raw('DAY(created_at)'), Carbon::today()->day)
)->count();
// $entry->callCount = ...
// $entry->leadCount = ...
}
// $view->with('TableB1',$TableB1);
This loop here could probably be avoided by directly including all the counts in your SQL, but anyway let's continue.
Now you can simply do this in your view:
'<tr>
<th scope="row">'. $data->groupname .'</th>
<th scope="row">'. $data->name .'</th>';
<th scope="row">'. $data->meetingCount .'</th>';
<th scope="row">'. $data->leadCount .'</th>';
...
Bonus: Since you use Laravel and Blade, why not use its Syntax?
<tbody>
@foreach($table as $entry)
<tr>
<th scope="row">{{$entry->groupname}}</th>
<th scope="row">{{$entry->name}}</th>
<th scope="row">{{$entry->meetingCount}}</th>
</tr>
@endforeach
</tbody>
No need for <?php
statements and lots of quote signs.