SpaceDogCS
SpaceDogCS

Reputation: 2968

Laravel collection changing key issue

I foundout that my collection were with the wrong keys, so I started debuging the code. Doing that I noticed that if I pass more than 2500 items in the array to the collection it loses their keys, otherwise, if I pass less than 2501 items it stays normal

Limiting to 2500 items

    public function getProdutos($codigoconcentrador, $codigoempresa, $pluck)
    {
        $model_produtos = new Produto();

        $query = $model_produtos->newQuery();

        $query->where('codigoconcentrador', $codigoconcentrador)
            ->where('codigoempresa', $codigoempresa)
            ->where('status', 'A');

        $query->limit(2500);

        dd($query->pluck('nomeproduto', 'codigopro'));

        if ($pluck) {
            return $query->pluck('nomeproduto', 'codigopro');
        } else {
            return $query->get();
        }
    }

Result

Collection {#260 ▼
  #items: array:2500 [▼
    1 => "CIMENTO VOTORAN CP II Z 32 - 50 KG"
    7 => "SOPA 250G - LEGUMES"
    8 => "SOPA 250G - CARNE"
    9 => "CIMENTO CIPLAN CP II Z 32 - 50 KG"
    10 => "ARGAMASSA VOTORAN ACI"
    11 => "ARGAMASSA VOTORAN ACII"
    13 => "ARGAMASSA COLA BEM ACI "
    14 => "ARGAMASSA COLA BEM ACII "
    ...
    ...

Without the limitation

    public function getProdutos($codigoconcentrador, $codigoempresa, $pluck)
    {
        $model_produtos = new Produto();

        $query = $model_produtos->newQuery();

        $query->where('codigoconcentrador', $codigoconcentrador)
            ->where('codigoempresa', $codigoempresa)
            ->where('status', 'A');

//        $query->limit(2500);

        dd($query->pluck('nomeproduto', 'codigopro'));

        if ($pluck) {
            return $query->pluck('nomeproduto', 'codigopro');
        } else {
            return $query->get();
        }
    }

Result

Collection {#3809 ▼
  #items: array:2875 [▼
    0 => "CIMENTO VOTORAN CP II Z 32 - 50 KG"
    1 => "SOPA 250G - LEGUMES"
    2 => "SOPA 250G - CARNE"
    3 => "CIMENTO CIPLAN CP II Z 32 - 50 KG"
    4 => "ARGAMASSA VOTORAN ACI"
    5 => "ARGAMASSA VOTORAN ACII"
    6 => "ARGAMASSA COLA BEM ACI "
    7 => "ARGAMASSA COLA BEM ACII "
    8 => "REATOR 2 X 40 BIVOLT"
    9 => "CHA VERDE SOLUVEL - LIMAO 200G - UND"
    10 => "ROLDANA P/ POCO GANCHO 12CM. R.C.A."
    ...
    ...

UPDATE

the function is receiving the second parameter $query->pluck('nomeproduto', 'codigopro'), so I debugged the pluck function, just when it becomes an collection it loses the key

vendor/laravel/framework/src/Illuminate/Support/Collection.php

public function pluck($value, $key = null)
    {
        dd(Arr::pluck($this->items, $value, $key)); // First Result
        dd(new static(Arr::pluck($this->items, $value, $key))); // Second Result
//        return new static(Arr::pluck($this->items, $value, $key));
    }

Upvotes: 3

Views: 3368

Answers (2)

Jeff
Jeff

Reputation: 166

The plug method search for a column with that name and uses it as the new key for each element... If you want just to take two columns of each element then you can use $collection->only('aa', 'bb'). Besides that, for many reasons you should be working with pagination... An array of 2500+ elements is crazy.

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

You're getting the first result when you pass a key as the second parameter:

pluck('nomeproduto', 'codigopro')

And you're getting the second one when you do not pass it:

pluck('nomeproduto')

So, double check the code.

Upvotes: 1

Related Questions