首页 > 解决方案 > Laravel Query Builder Issue Remove Null Values

问题描述

Hi all you smart individuals i'm hitting a solid wall here with someone else code that i'm trying to fix a issue.

The issue is that when this query builder has got all the results there seems to be some coming back with null values and i'm not sure how to remove them before I paginate the data, I know how to do it if it was a collection however maybe some of you might be-able to help me.

so currently the logic goes into this pagination

$return = $tld->paginate($request->get('limit'))->toArray();

$tld being the eloquent builder.

Which then gets sent into this function that was created..

$return = $this->makePagination($return);

public function makePagination($object = [], $filters = []) {
        return [
            'data' => [
                'items' => $object['data'],
                'pagination' => [
                    'from' => $object['from'],
                    'to' => $object['to'],
                    'total' => $object['total'],
                    'per_page' => $object['per_page'],
                    'first_page' => [
                        'number' => 1,
                        'url' => $object['first_page_url']
                    ],
                    'last_page' => [
                        'number' => $object['last_page'],
                        'url' => $object['last_page_url']
                    ],
                    'next_page' => [
                        'number' => $object['current_page'] + 1,
                        'url' => $object['next_page_url']
                    ],
                    'prev_page' => [
                        'number' => $object['current_page'] - 1,
                        'url' => $object['prev_page_url']
                    ]
                ],
                'params' => $filters
            ]
        ];
    }

But then i'm getting a response like this with Null values and I would like to remove them before any of this pagination happens

 {
    "data": {
        "items": [
            {
                "id": 13771,               
            },
            null,
            {
                "id": 4125,                
            },

Side note if I run $tld->get() I can see all the results and there are null values in there so if anyone can show me how to remove the null values that would be a great help <3 you all if you can help me ...

Update

Ive also tried $tld->get()->filter(); and thats also not removing the null values I still get this response

[
    {
        "id": 13771,
    },
    null,
        {
        "id": 789,
    }
]

标签: laraveleloquentbuilder

解决方案


我想我用一点技巧修复了它

$filtered = collect(array_values(array_filter($tld->get()->toArray())));

return $this->paginate($filtered, $request->get('limit') ?? 15 , $page = null, $options = []);

然后创建了一个集合分页器

public function paginate($items, $perPage = 15, $page = null, $options = [])
{
    $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);

    $items = $items instanceof Collection ? $items : Collection::make($items);

    return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}

推荐阅读