2017-09-14 6 views
0

맞춤 설정 조건에 따라 그룹을 만들고 싶습니다.하지만 내 방식이 옳은지 확실하지 않습니다.레벨 그룹이 제대로 작동하지 않음

클로저를 좋아하지 않는 내 코드

$cderList = $cderListQuery->groupBy('cder_id') 
    ->groupBy('name') 
    ->groupBy(function ($query) { 
     if(!is_null($this->hp) || !is_null($this->hp)) 
     { 
      $query->groupBy('Value_Float'); 
     } 

    }) 
    ->orderBy('introduced', 'DESC') 
    ->limit(20) 
    ->get(); 

오류 세부 orderBy 이후

strtolower() expects parameter 1 to be string, object given 
in Grammar.php (line 58) 

at HandleExceptions->handleError(2, 'strtolower() 
expects parameter 1 to be string, object given', 
'/home/vendor/laravel/ 
framework/src/Illuminate/Database/Grammar.php', 
58, array('value' => object(Closure), 'prefixAlias' => false)) 
+0

어떤 오류가 나타 납니까? – wbail

+0

오류 세부 정보 업데이트 된 @wbail – testgo

+0

이 orderBy() 절의 문제가있는 것 같습니다. –

답변

0

사용을 때 조건절 (laravel 설명서에서) 나는 것 같아요 요 원하는 경우에만 'Value_Float'에 의한 주문입니다.

//Set the condition here 
$condition = !is_null($this->hp) || !is_null($this->hp); 

$cderList = $cderListQuery->groupBy('cder_id') 
    ->groupBy('name') 
    ->when($condition, function($query){ 
     //This segment will only run if condition is true 
     return $query->groupBy('Value_Float'); 
    }) 
    ->orderBy('introduced', 'DESC') 
    ->limit(20) 
    ->get(); 
2

, 당신은 조금 같은 쿼리를 분리 할 수 ​​있습니다 :

$query = $cderListQuery->groupBy('cder_id') 
    ->groupBy('name'); 

if(!is_null($this->hp) || !is_null($this->hp)) 
{ 
    $query->groupBy('Value_Float'); 
} 

$cderList = $query->orderBy('introduced', 'DESC') 
    ->limit(20) 
    ->get();