I have some data i have stored in mysql and it looks like this
["1109255736625955df9fbef.jpg","707361535625955dfa0564.jpg","1126563303625955dfa0c5c.jpg"]
i stored this data as an array in a field called product_images
that holds strings.
Is there a way of iterating through this data without making it a php array at first but instead iterate it in blade straight from the database?.
This doesn't work
<div >
@foreach ($product->product_images as $image)
<div >
{{$image}}
</div>
@endforeach
</div>
CodePudding user response:
you should use mutators to cast array
Product in model add:
protected $casts = [
'product_images' => 'array',
];
The cast from string to array is done under the hood for you
CodePudding user response:
Your data is stored in database as string, so you should transform it to array. Convenient way to do that is creating Cast in your model. Thanks this you transform data from this column to array on fetch and transforming to json when stored.
protected array $casts = [
'product_images' => 'array'
]