2016-11-03 9 views
0

配列の値のリストと列を一致させるためのラベールの雄弁なクエリから結果セットを取得しようとしています。Laravelから配列結果セットを取得する方法

$authenticated_operation_ids = AccessControl::where('user_id', '=', $user_id) 
    ->where('entity_type_id', '=', $operation_entity_id) 
    ->pluck('entity_access_id')->toArray(); 
$authenticated_operations = Operation::whereIn('id', $authenticated_operation_ids); 

return view('page.index')->withOperations($authenticated_operations); 
+0

あなたはクエリの最後に ' - > get()'を忘れています。 – aynber

+0

mongodbでは、ネイティブIDは_idプロパティとして作成されます。 IDを意図的に変更しなかった場合は、idを_idに変更する必要があります。 – zarax

答えて

0

あなたはとしてそれを試すことができます:クエリの最後にget()を追加

$authenticated_operation_ids = AccessControl::where('user_id', '=', $user_id)->where('entity_type_id', '=', $operation_entity_id)->pluck('entity_access_id')->toArray(); 
$authenticated_operations = Operation::whereIn('id', $authenticated_operation_ids)->get(); 
return view('page.index')->withOperations($authenticated_operations); 

0

1)pluckは、単一の行から単一の値を返します。 listsに複数の行から1つの列を取得させたいとします。それは値の配列を返すようtoArrayは、必要とされなくてもよい

$authenticated_operation_ids = AccessControl::where('user_id', '=', $user_id) 
    ->where('entity_type_id', '=', $operation_entity_id) 
    ->lists('entity_access_id'); 

2)あなたが実際にあなたの2行目に行を取得するために忘れている:

$authenticated_operations = Operation::whereIn('id', $authenticated_operation_ids) 
    ->get(); 
あなたが得る呼び出す必要があり
+1

Laravel 5.2では、コレクション、クエリービルダー、およびEloquentクエリービルダーオブジェクトの 'lists'メソッドの名前が' pluck'に変更されました。 [Docs](https://laravel.com/docs/5.3/upgrade#upgrade-5.2.0) –

+0

ああ、ありがとう!そうすれば、#1の問題がレンダリングされます。少なくとも5.2を使用している場合。バージョンは言及されていません。 – aynber

0

()結果を得るために結果セット上の関数。変更されたコードは

$authenticated_operation_ids = AccessControl::where('user_id', '=', $user_id)->where('entity_type_id', '=', $operation_entity_id)->get()->pluck('entity_access_id'); 
$authenticated_operations = Operation::whereIn('id', $authenticated_operation_ids)->get(); 
return view('page.index')->withOperations($authenticated_operations); 

のようになります。また、カーソルを使って処理することもできます。

関連する問題