我的后续查询是:
$rows = ProjectLogin::with(
'User:PRIVATE,naam',
'Project:PRIVATE,naam',
'Queue:PRIVATE,queue_naam',
'AgentStatus:PRIVATE,status'
)
->limit(10)->get();
如果我执行$rows->toArray((:
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((而不是嵌套数组的查询中获取特定列
我找到了两个解决问题的方法
第一种解决方案是接受嵌套的关系,并确保它们返回带有"的默认字段;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过时的新查询。
回答问题"如何从使用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()
变量$collection
保存了您雄辩的收集结果。