如何从Laravel获取数组结果集的条件

时间:2016-11-03 14:28:47

标签: php arrays laravel where-in

我正在尝试从laravel eloquent查询中获取结果集,我将列与数组中的值列表进行匹配。

$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);

3 个答案:

答案 0 :(得分:0)

您可以尝试:

$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);

在查询末尾添加get()

答案 1 :(得分:0)

1)pluck从单行返回单个值。您希望lists从多行获取单个列。可能不需要toArray,因为它返回值数组

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

2)您忘记在第二行中实际检索行:

$authenticated_operations = Operation::whereIn('id', $authenticated_operation_ids)
    ->get();

答案 2 :(得分:0)

您必须在结果集上调用get()函数才能获得结果。修改后的代码就像

$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);

或者您可以使用游标来处理它。