2017-09-14 3 views
0

私は自分のカスタム条件に依存して1つのグループを作っていきたいと思います。しかし、自分の道が正しいかどうかはわかりません。ラーバルグループが正常に動作していません

クロージャを好きではない私のコード

$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(); 
関連する問題