Home > Mobile >  Transfer the contents of a file to a collection
Transfer the contents of a file to a collection

Time:11-17

How can I transfer the contents of a file to a collection

File Contents test.txt

[
    {"nmId":40699165,"price":924,"discount":0,"promoCode":0},
    {"nmId":40384610,"price":4155,"discount":0,"promoCode":0}
]

I get it like this

$contents = Storage::get('test.txt');

I want to get a collection in view at the output

[
  ['nmId' => '40699165', 'price' => 924, 'discount' => 0, 'promoCode' => 0],  
  ['nmId' => '40384610', 'price' => 4155, 'discount' => 0, 'promoCode' => 0],  
]

CodePudding user response:

I would use the json_decode() function assuming the file is formatted correctly.

$contents = json_decode(Storage::get('test.txt'), true);

If you want a laravel collection

$collection = collect($contents);
  • Related