0

I'm having a hard time to read multiple file paths from my product tables in laravel.

I'm using Voyager as my admin interface, I have a multiple file upload that saves my path image in a field, when i query the table i get these result:

Example

how do i read the photo field?

Already tried: json_decode($product->photo,true) and it says "htmlspecialchars() expects parameter 1 to be string, array given"

with @foreach ($product->photo as $img) it gives me "Invalid argument supplied for foreach() "

2
  • 1
    can you show in your model class.look like you have added $casts for that field as array Commented Nov 21, 2021 at 14:05
  • @JohnLobo tkz for your comment, already solve using $casts in the model as you said Commented Nov 21, 2021 at 15:47

1 Answer 1

2

photo column must be defined as JSON in its migrations file.

$table->json('photo');

And cast it to array in Product Model class:

protected $casts = [
   "photo" => "array"
];

Now you will access photo as array and do what you want.

Sign up to request clarification or add additional context in comments.

1 Comment

Tkz a lot... it worked like a charm... ;)

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.