2015-07-20 39 views
1

我要檢查的總和值和laravel 4.2查詢的WHERE條件計數值怎麼樣查詢中使用與計數值在MySQL中laravel 4.2

下面的代碼,我有

$aColumns = array('driver_details.DriverId', DB::raw('CONCAT(driver_details.Firstname, " ", driver_details.Lastname, "/", driver_details.TaxiPlateNo) AS TaxiDriver'), DB::raw('count(taxi_trip.AutoId) AS TotalTrip'), DB::raw('sum(taxi_trip.TripDistance) AS TotalTripDistance'), DB::raw('sum(taxi_trip.TotalFare) AS TotalTripFare'));  
    $aColumnsSearch = array('driver_details.DriverId', 'driver_details.Firstname', 'driver_details.Lastname','driver_details.TaxiPlateNo', 
    DB::raw('count(taxi_trip.AutoId)')); 

$Search=''; 
if(Input::get('Search')!='') 
    $Search = Input::get('Search');  
$DriverId=''; 
if(Input::get('DriverId')!='') 
    $DriverId = Input::get('DriverId'); 

$IsCancel=''; 
if(Input::get('IsCancel')!='') 
    $IsCancel = Input::get('IsCancel'); 

$FromDate=''; 
if(Input::get('FromDate')!='') 
    $FromDate = Input::get('FromDate'); 

    $ToDate=''; 
    if(Input::get('ToDate')!='') 
     $ToDate = Input::get('ToDate'); 

$tempqry = DB::connection()->table('driver_details')  
    ->select($aColumns) 
    ->leftJoin('taxi_trip', function($join) 
      { 
      $join->on('taxi_trip.DriverId', '=', 'driver_details.DriverId'); 
      $join->on('taxi_trip.PickupLAT', '!=', DB::raw(0)); 
      $join->on('taxi_trip.DropLAT', '!=', DB::raw(0)); 
      $join->on('taxi_trip.TotalFare', '!=', DB::raw(0)); 
      }) 
     ->where(function($query) use ($FromDate) { 
     if($FromDate!='') 
     { 
      $query->where(DB::raw("date_format(taxi_trip.RequestDate,'%Y-%m-%d')"), '>=', date("Y-m-d",strtotime($FromDate))); 
     } 
    }) 
    ->where(function($query) use ($ToDate) { 
     if($ToDate!='') 
     { 
      $query->where(DB::raw("date_format(taxi_trip.RequestDate,'%Y-%m-%d')"), '<=', date("Y-m-d",strtotime($ToDate))); 
     } 
    }) 
    ->where(function($query) use ($Search, $aColumnsSearch) 
      { 
       for ($i=0 ; $i<count($aColumnsSearch) ; $i++) 
       { 
        //$query->orWhere($aColumnsSearch[$i].' LIKE %'.$Search.'%'); 

        $query->orWhere($aColumnsSearch[$i],'LIKE', '%'.$Search.'%'); 
       } 
      }) 
     ->where(function($query) use ($DriverId) { 
     if($DriverId!='') 
     { 
      $query->where('taxi_trip.DriverId', '=', $DriverId); 
     } 
    }) 

    ->groupby('driver_details.DriverId'); 

    $temp = $tempqry; 
    $tempqry = $temp->get(); 

    return $tempqry; 

我希望使用從查詢獲得的count()和sum()值與我正在給的搜索值

+0

您嘗試迄今爲止達到此目的? –

回答

0

我不知道您遵循表結構,但這裏的工作邏輯,你期待什麼。

第1步:

獲取用戶的輸入,並獲得符合您盤點記錄這將是這個樣子的列表。

$inputCount = '100'; #Assign the user input here 
$result = DB::table('users') 
        ->orderBy('name', 'desc') 
        ->groupBy('count') 
        ->having('count', '>', $inputCount) 
        ->get(); 

現在,您將獲得具有與用戶輸入相匹配的總數的用戶列表。

注:count比總數不同

第2步:

只是重複使用foreach

@foreach ($result as $data) 
    <p>This is user {{ $data->id }}</p> 
@endforeach 

注:如果你使用的數據表,那麼你應該迭代你的表結構。

補充一點:

如果您需要檢索用戶的信息是迭代裏面即可。

你必須進去迭代

即拔迭代實體的詳細信息,用戶的數據

$name = DB::table('users')->where('id', $result->id)->pluck('name'); 

所以,你有權

@foreach ($result as $data) 
<p>This is user {{ $data->id }}</p> 
$name = DB::table('users')->where('id', $data->id)->pluck('name'); 
@endforeach 

建議:

我會推薦結束您使用eloquent

0

你試過這樣嗎?

public function myfunction($query, $tag) 
{ 
    return $query->where('title', 'LIKE', '%'.$tag.'%'); 
} 

MyTable::where('name', 'LIKE', '%'.$name.'%')->get(); 

MyTable::like('name', 'Praveen')->get(); 
+0

這不起作用 – sujivasagam