Laravel eloquent with() 父级未嵌套的嵌套关系字段

Laravel eloquent with() nested relationship fields in parent unnested

我的查询是:

$rows = ProjectLogin::with(
    'User:PRIVATE,naam', 
    'Project:PRIVATE,naam', 
    'Queue:PRIVATE,queue_naam', 
    'AgentStatus:PRIVATE,status'
)
->limit(10)->get();

如果我执行 $rows->toArray():

,这将 return 这个数组
array:10 [▼
  0 => array:18 [▶]
  1 => array:18 [▼
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => "PRIVATE"
    "PRIVATE" => "PRIVATE"
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "user" => array:2 [▼
      "PRIVATE" => PRIVATE
      "naam" => "SOME NAME"
    ]
    "project" => array:2 [▼
      "PRIVATE" => PRIVATE
      "naam" => "SOME PROJECT NAME"
    ]
    "queue" => null
    "agent_status" => array:2 [▼
      "PRIVATE" => PRIVATE
      "status" => "SOME STATUS"
    ]
  ]
  2 => array:18 [▶]
  3 => array:18 [▶]
  4 => array:18 [▶]
  5 => array:18 [▶]
  6 => array:18 [▶]
  7 => array:18 [▶]
  8 => array:18 [▶]
  9 => array:18 [▶]
]

withs 在数组中,但我不想要嵌套数组,而且它包含我不需要的外键。

我希望数组看起来像例如:

array:10 [▼
  0 => array:18 [▶]
  1 => array:18 [▼
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => "PRIVATE"
    "PRIVATE" => "PRIVATE"
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "PRIVATE" => PRIVATE
    "user_naam" => 'SOME NAME'
    "project_naam" => 'SOME NAME'
    "agent_status_status" => 'SOME STATUS'
    
  ]
  2 => array:18 [▶]
  3 => array:18 [▶]
  4 => array:18 [▶]
  5 => array:18 [▶]
  6 => array:18 [▶]
  7 => array:18 [▶]
  8 => array:18 [▶]
  9 => array:18 [▶]
]

如何从在主数组而不是嵌套数组中使用 with() 的查询中获取特定列?

回答问题“如何从使用 with() 的查询中获取特定列?”

尝试用这样的回调函数编写 with()

$rows = ProjectLogin::with( 'User:PRIVATE,naam', function($query){
    $query->select('column_name');
})
->with( 'Project:PRIVATE,naam', function($query){
    $query->select('column_name');
})
->with( 'Queue:PRIVATE,queue_naam', function($query){
    $query->select('column_name');
})
-> with( 'AgentStatus:PRIVATE,status', function($query){
    $query->select('column_name');
})

请注意我如何将 with() 分成不同的行,这是因为当您将回调函数传递给该函数时,它只能接受一个参数。

关于数组格式的问题,我不确定这是否可行,但请尝试将它们放入一个集合中并展平该集合以获得所需的状态。

$flattened = $collection->flaten()

保存您的 eloquent 收集结果的变量 $collection

我找到了 2 个解决我的问题的方法。

第一个解决方案是接受嵌套关系并确保它们 return 使用“withDefault()”方法的默认字段。看起来像:

$rows = ProjectLogin::
with([
    'User' => function($query) { 
        $query->select('PRIVATE','naam')
        ->withDefault(['PRIVATE' => '','naam' => '']); 
    },
    'Project' => function($query) { 
        $query->select('PRIVATE','naam')
        ->withDefault(['PRIVATE' => '','naam' => '']); 
    },
    'Queue' => function($query) { 
        $query->select('PRIVATE','queue_naam')
        ->withDefault(['PRIVATE' => '','queue_naam' => '']); 
    },
    'AgentStatus' => function($query) { 
        $query->select('PRIVATE','status','status_colour')
        ->withDefault(['PRIVATE' => '','status' => '','status_colour' => '']); 
    },
]
)->get();

另一个解决方案是,如果嵌套数组不是一个选项,则使用“map()”,它看起来像:

$rows = ProjectLogin::with(
    'User:PRIVATE,naam', 
    'Project:PRIVATE,naam', 
    'Queue:PRIVATE,queue_naam', 
    'AgentStatus:PRIVATE,status,status_colour'
)
->get()
        ->map(function($item) {
            $item = $item->toArray();
            return [
            'PRIVATE' => $item['PRIVATE'],
            'PRIVATE' => $item['PRIVATE'],
            'PRIVATE' => $item['PRIVATE'],
            'PRIVATE' => $item['PRIVATE'],
            'user_name' => @$item['user']['naam'],
            'project_name' => @$item['project']['naam'],
            'queue_name' => @$item['queue']['queue_naam'],
            'status' => @$item['agent_status']['status'],
            'PRIVATE' => $item['PRIVATE'],
            'status_colour' => @$item['agent_status']['status_colour'],
            ];
        });

$item = $item->toArray(); 很重要,否则会创建使 n+1 过时的新查询。