2016-11-15 4 views
2

私は多くの関係でピボットテーブルにアクセスするこのコントローラを持っています。ピボットテーブルの列は値で、created_atです。私は(あなたがクエリから配列を返す必要がある場合は、あなたの代わりにselect()laravel、配列を返すようにコードを変更する方法は?

$o_response = $this->statuses()->where('status_id', $o_health_status->id) 
    ->pluck('values','created_at')->orderBy('created_at','ASC');// ->first(); 
//I think you no need to use ->first(); here. 

Docspluck()を使用することができ、このコントローラは、私の値の配列を返すとlaravelで

public function status($s_type) 
{ 

    $o_health_status = Status::where('name', 'health')->first(); 
    $o_speed_status = Status::where('name', 'speed')->first(); 

    if ($s_type === 'health'){ 

     $o_response = $this->statuses()->where('status_id', $o_health_status->id) 
     ->select('values','created_at')->orderBy('created_at','ASC')->first(); 

     if($o_response === null){ 
      return 'unsigned'; 
     } 
     $o_response = $o_response->values; 
     return $o_response;   
    }else{ 

     $o_response = $this->statuses()->where('status_id', $o_speed_status->id) 
     ->select('values','created_at')->orderBy('created_at', 'desc')->first(); 
     if($o_response === null){ 
      return 'unsigned'; 
     } 

     $o_response = $o_response->values; 
     return $o_response; 


    } 
+1

あなたが何を求めているのかははっきりしていません。同じ理由で – manniL

答えて

0

をcreated_atとしたいですにスクロールします。列値のリストを取得する

+0

、あなたが提案したコードを書いたとき。それは私には、メソッドorderByは存在しないと言うエラーを与えた! 。あなたは同時にpluckとorderdByを使うことが許されていますか? –

+0

通常、得られた配列は、データベースにある値に従って配列されます。彼らが注文されれば、心配することはありません。同じ理由で – Sachith

1

これを行うには、pluckを使用する必要があります。コレクションから必要な要素がすべて抜け落ちるので、少しきれいにして、次のように見えるはずです:

public function status($s_type) 
{ 

    $o_health_status = Status::where('name', 'health')->first(); 
    $o_speed_status = Status::where('name', 'speed')->first(); 
    $o_response = $s_type === 'health' ? $this->statuses()->pluck('values','created_at')->where('status_id', $o_health_status->id) 
      ->select('values', 'created_at')->orderBy('created_at', 'asc')->first() : $this->statuses()->pluck('values','created_at')->where('status_id', $o_speed_status->id) 
      ->select('values', 'created_at')->orderBy('created_at', 'desc')->first(); 

    return ! $o_response ? 'unsigned' : $o_response->values; 
} 
+0

のコードは完璧に見えますが、自分のコードを自分のコードに置き換えたときです。それはまったく動作しません。 –

+0

が動作しない場合は、答えとしてマークした理由は?再度確認してください。 – Sachith

関連する問題