2

I'm working with laravel and made little api. I have problems with keyBy(). I get response like this

"translate": [
    0: {"id": 0, "lang": "az" },
    1: {"id": 0, "lang": "ru" },
    2: {"id": 0, "lang": "en" }
]

And I want to change keys of these array like this:

"translate": [
    "az": {"id": 0, "lang": "az" },
    "en": {"id": 0, "lang": "ru" },
    "ru": {"id": 0, "lang": "en" }
]

with that snippet

foreach ($categories as $key => $row) {
    if ($row) {
        $row->translate = $row->translate->keyBy('lang');
    }
}

But results are same. Array keys don't change. I don't know why. I just dd($row->translate) and I got result what I want, but the last results are same as before. Thanks!

julianstark999
  • 3,450
  • 1
  • 27
  • 41
rufatZZ
  • 743
  • 2
  • 11
  • 27

2 Answers2

0

It should work without any problem, I believe the problem is somewhere else. Take a look at this example:

$object = new stdClass();

$object->translate = collect([
    0 => (object) ['id' => 0, 'lang' => 'az'],
    1 => (object) ['id' => 0, 'lang' => 'ru'],
    2 => (object) ['id' => 0, 'lang' => 'en'],
]);

$object2 = new stdClass();

$object2->translate = collect([
    0 => (object) ['id' => 0, 'lang' => 'x'],
    1 => (object) ['id' => 0, 'lang' => 'y'],
    2 => (object) ['id' => 0, 'lang' => 'z'],
]);

$categories = [$object, $object2];    

foreach ($categories as $key => $row) {
    if ($row){
        $row->translate = $row->translate->keyBy('lang');
    }
}    

dd($categories);

As a result there is:

array:2 [▼
  0 => {#329 ▼
    +"translate": Collection {#346 ▼
      #items: array:3 [▼
        "az" => {#335 ▼
          +"id": 0
          +"lang": "az"
        }
        "ru" => {#337 ▼
          +"id": 0
          +"lang": "ru"
        }
        "en" => {#338 ▼
          +"id": 0
          +"lang": "en"
        }
      ]
    }
  }
  1 => {#340 ▼
    +"translate": Collection {#345 ▼
      #items: array:3 [▼
        "x" => {#341 ▼
          +"id": 0
          +"lang": "x"
        }
        "y" => {#342 ▼
          +"id": 0
          +"lang": "y"
        }
        "z" => {#343 ▼
          +"id": 0
          +"lang": "z"
        }
      ]
    }
  }
]

so as you see results were changed

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • This translation main part of my `json` object. I have to separate this for use inside collection and put it back ? – rufatZZ Dec 19 '17 at 12:04
  • Sorry, I don't know what you mean but as I showed you it should work without any problem if you are using collections, compare mine code with yours and make sure you verify valid result – Marcin Nabiałek Dec 19 '17 at 12:05
  • Yeah, I`m working on it. :) Thanks for your reply :) – rufatZZ Dec 19 '17 at 12:12
  • I thought when I `dd(response)` all keys showed up, but when I commented `dd(response)` and send main json, all keys gone in `json`. I don't know whats wrong. I will attach screenshots of `dd()` and `json` results. – rufatZZ Dec 19 '17 at 12:27
0

translate inside dd($data)

enter image description here

translate inside json object (api response)

enter image description here

I don't know what changes keys.

rufatZZ
  • 743
  • 2
  • 11
  • 27
  • First of all you should update your question. In addition it doesn't tell us what exactly you do, how you return response and so on. – Marcin Nabiałek Dec 19 '17 at 12:41
  • My question is up to date. I just want keys shows up when I send `api` response. At the moment, I can't get it. I return response like that: `return response()->make($data);` and receive it `HomeController` then prepare it for view. – rufatZZ Dec 19 '17 at 12:47
  • Great. And how can we know what is data here? It's not included in your question! In your question you make something with $categories and not with $data – Marcin Nabiałek Dec 19 '17 at 12:51
  • `$data` is collection of `$categories` I named it `$data`. And I put here part of it which named `translate`. I can't attach here 200+ lines of json or Controller :) Main function which makes array I added it here before. – rufatZZ Dec 19 '17 at 12:55
  • We cannot help you if the error is in other part of code that you have not included in your question. – Marcin Nabiałek Dec 19 '17 at 12:58
  • Thanks all of you for your help. :) I don't get any error when i run this code. I just lose keys. – rufatZZ Dec 19 '17 at 13:00
  • So as I said the problem is in the part of code you didn't show. – Marcin Nabiałek Dec 19 '17 at 13:21