我有一张像这张活动表的桌子:qazxsw poi
我用Controller中的代码将数据从数据库中提取到表中:
在index.blade.php中
$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;
如何将php刀片中的代码移动到控制器? 代码有效,但我认为代码应该在控制器中吗? 如果可能的话,有一种方法可以通过AJAX按月和年来过滤表中的数据,这样我就不必刷新页面了吗?
我正在使用Laravel 5.7
在您的控制器中,您可以执行以下操作:
<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>
这里的循环可以通过直接包含SQL中的所有计数来避免,但无论如何让我们继续。
现在您可以在视图中执行此操作:
// $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);
奖励:既然你使用Laravel和Blade,为什么不使用它的语法呢?
'<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>';
...
不需要<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>
声明和许多引号。