我是Laravel的新手,我使用Laravel 5作为我的项目。 我的规格是根据培训类别计算前培训的数量,所以我使用了join和group by和count函数来获得培训前的数字:
在我的控制器代码中如下: -
<?php
namespace App\Http\Controllers;
use Request;
use App\Training;
use App\Schedule;
use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\TrainingRequest;
use Carbon\Carbon;
class TrainingController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function __construct(){
$this->middleware('auth',['only'=>'index']);
}
public function index()
{
//
$training=Training::all();
$before_held=Schedule::join('training','training.id','=','schedule.training_id')
->groupBy('training.category')->where('schedule.training_end_date','>=',carbon::now())->get();
// echo count($before_held);
//die();
return view('Training.index',compact('training','before_held'));
}
在视图中: -
<td><a href="/training/before_held">{{count($before_held)}}</a></td>
这总是在表格中显示0计数: -
|category|Held Before|
|test | 0 |
|test | 0 |
但是如果training_end_date大于或等于当前时间,我需要计算两次。
所以如果有人能解决这个问题我很感激。