2016-06-13 92 views
1

我是新来的laravel,我想使用多个where子句并使用curdate()。CURDATE()与laravel 5

这是一个例子:

$data = DB::table('toutcome')->where('date', '>=', curdate())->where('Status', '=', 'A')->count('AppID'); 

return view('home', compact('data')); 

它不工作。

+0

尝试请'andWhere()'的第二个条件 –

回答

0

因此,与在评论答案一起:

public function index() 
    { 
     // First query with DB::raw() variant 
     $data = DB::table('toutcome') 
      ->where('date', '>=', DB::raw('curdate()')) 
      ->where('Status', '=', 'A') 
      ->count('AppID'); 

     // Second query with Carbon variant 
     $data2 = DB::table('toutcome') 
      ->where('date', '>=', Carbon::now()) 
      ->where('Status', '=', 'A') 
      ->count('AppID'); 

     // Third query with '@Sunny' whereRaw variant 
     $data3 = DB::table('toutcome') 
      ->whereRaw('date >= curdate()') 
      ->where('Status', '=', 'A') 
      ->count('AppID'); 

     return view('home', compact('data','data2','data3')); 
    } 

我不紧凑的()个人,所以我会写一个大风扇:

return view('home', ['data'=>&$data,'data2'=>&$data2,'data3'=>&$data3]) 

虽然个人(如果你想阅读更进一步)阅读在ViewComposers:

https://laravel.com/docs/master/views#view-composers

1

变化 where('date', '>=', curdate())whereRaw('date >= curdate()')

+0

其他可能性:'在哪里( '日', '> =',碳::现在())'和'在哪里( 'date','> =',DB :: raw('curdate()'))' –

+1

谢谢,这也教会了我一件事! :) –

+1

@SunnyRGupta,谢谢你,谢谢Justion,你可以看看更新的问题。 – Matt