0

i have an array which is like below :

"["events/81.jpg","events/Ancient.2.jpg","events/post.jpg"]"

this is the result of a dd now what i want to do is to change this to a collection with 3 objects so i can loop into that and show it in front end . how can i do that what i tried was like below :

explode(',','$media');

but this brings the [ charachter too so i cant show it in front end . the full result of the dd is as below :

attributes: array:11 [▼
    "id" => 1
    "title" => "test"
    "description" => "<p>test</p>"
    "teaser" => "events\December2020\G0SiKVIYomU9SMuuE8ux.jpg"
    "media" => "["events/88c7ae4a-8a0a-467f-b09c-0f50206a35eb.jpg","events/Ancient.Aliens.S14E05.480p.Film2Movie_WS.mkv_snapshot_06.45.jpg","events/post.jpg"]"
    "price" => 11998
    "start_at" => "2020-12-28 00:00:00"
    "finish_at" => "2020-12-28 00:00:00"
    "is_free" => 1
    "created_at" => "2020-12-28 11:15:00"
    "updated_at" => "2020-12-28 11:30:41"
  ]

1 Answer 1

1

That's not an array, it's a string representation of one. You can json_decode it:

json_decode($media)

Output:

Array
(
    [0] => events/81.jpg
    [1] => events/Ancient.2.jpg
    [2] => events/post.jpg
)
Sign up to request clarification or add additional context in comments.

2 Comments

ok cool i see it now thanks can i convert it object from string or i should do it after changing to array
@Farshad you can convert it into a collection after it's been converted to an array. e.g. $collection = collect(json_decode($media));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.