从查询生成器中删除空值 Laravel
Remove Nulls From Query Builder Laravel
当 运行 一个 Query Builder 然后对数据进行分页时 return 我得到的一些数据有点麻烦
我想知道是否有人知道如何在对数据进行分页之前删除 Null 结果。
// So Before I hit this
$return = $tld->paginate($request->get('limit'))->toArray();
这是我分页后的问题
array:12 [
"current_page" => 1
"data" => array:12 [
0 => null
1 => array:3 [
我需要摆脱那些 Null 值,我知道在分页后该怎么做,但是我想在分页之前摆脱它们...
希望各位laravel大神帮帮我..
为 $tld 添加了这个额外的逻辑
private function newest(Request $request)
{
$this->validate($request, [
'timescale' => [
'required',
Rule::in(['today', 'this_week', 'this_month', 'all_time'])
]
]);
$tld = TimelineItem::where('timeline_items.created_at', '>', $this->timescaleToHours($request->get('timescale')));
if ($request->search_content) {
$tld = $this->searchContent($tld, $request->search_content, 0.4);
} else {
$tld = $tld->orderBy('timeline_items.created_at', 'DESC');
}
if ($request->types) {
$tld = $this->filters($tld, $request->types);
}
if ($request->tags) {
$tld = $this->tags($tld, $request->tags);
}
return $tld;
}
您可以使用 filter() 删除 null。 Laravel Collection filter()
$response = $tld->paginate($request->get('limit'))->toArray();
$response['data'] = collect($response['data'])->filter()->toArray();
return $response;
当 运行 一个 Query Builder 然后对数据进行分页时 return 我得到的一些数据有点麻烦
我想知道是否有人知道如何在对数据进行分页之前删除 Null 结果。
// So Before I hit this
$return = $tld->paginate($request->get('limit'))->toArray();
这是我分页后的问题
array:12 [
"current_page" => 1
"data" => array:12 [
0 => null
1 => array:3 [
我需要摆脱那些 Null 值,我知道在分页后该怎么做,但是我想在分页之前摆脱它们...
希望各位laravel大神帮帮我..
为 $tld 添加了这个额外的逻辑
private function newest(Request $request)
{
$this->validate($request, [
'timescale' => [
'required',
Rule::in(['today', 'this_week', 'this_month', 'all_time'])
]
]);
$tld = TimelineItem::where('timeline_items.created_at', '>', $this->timescaleToHours($request->get('timescale')));
if ($request->search_content) {
$tld = $this->searchContent($tld, $request->search_content, 0.4);
} else {
$tld = $tld->orderBy('timeline_items.created_at', 'DESC');
}
if ($request->types) {
$tld = $this->filters($tld, $request->types);
}
if ($request->tags) {
$tld = $this->tags($tld, $request->tags);
}
return $tld;
}
您可以使用 filter() 删除 null。 Laravel Collection filter()
$response = $tld->paginate($request->get('limit'))->toArray();
$response['data'] = collect($response['data'])->filter()->toArray();
return $response;