I want to save images in db like:
{
"1":"accounts/29cc60f2-c018-403c-80a9-94cda509e0f7.jpeg",
"2":"accounts/c2658592-526a-426e-9834-084e0252c4c5.jpeg",
"3":"accounts/3db1d928-e32f-4f3e-b41e-c99ffefec952.jpeg",
"4":"accounts/4d421a36-dc7e-4252-b806-17467a515aa7.jpeg"
}
but in response just recieve one data in output in db like :
["property/March2023/535856080.png"]
in my code :
in model :
protected $fillable
= [
'images',
];
protected $casts
= [
'images' => 'array',
];
in controller :
$image_exists = $request->file('images');
if ($image_exists !== Null) {
$images = array();
$date = now()->format('F') . now()->format('Y');
if ($files = $request->file('images')) {
foreach ($files as $file) {
$name = rand() . '.' . $file->getClientOriginalExtension();
$file->store('image', 'public');
$images[] = 'property/' . $date . '/' . $name;
}
}
dd($images);
$property = DB::table('re_properties')->orderBy('id', 'DESC')->first();
DB::table('re_properties')->where('id', $property->id)->update([
'images' => $images,
]);
} else {
}
2
Answers
you must first use implode() to save the images and save like in
Try this:
and for get images use explode() and get data like in
Try this:
When you save data, you can use serialize() to convert array to string & also use base64_encode() to encode data.
When you fetch data, you can us unserialize() & base64_decode() to decode data & convert string to array.