R
R
RMate2019-03-27 11:16:26
Laravel
RMate, 2019-03-27 11:16:26

How to select only specified keys from a collection in Laravel?

Hello!
You must select certain keys from the collection. The documentation describes that this can be done using the only method. What am I doing wrong?
We have a collection like:

$filter = collect([
    'q' => 'q', 
    'w' => 'w', 
    'e' => [
      'a' => 'a', 
      's' => 's'
    ]
  ]);

As far as I know, to access a nested array, you can use the dot notation only(['ea', 'es'])
for the reverse except method - this option works correctly, in the case of only - it does not work. either I can't see the elephant, or it's a version bug.
By the way, the version of laravel is 5.7
CHADNT?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor, 2019-03-27
@RMate

Well, you would have climbed into the source code or something ...
This method uses Arr::only, which simply calls array_intersect_key, which does not support any dots in the keys. It looks like it's only manually choosing from a nested array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question