简体   繁体   中英

replace a key value in array with a new value

I have a form with this fields in laravel:

<form>
<input type='text' name="title">
<input type='file' name="files">
</form>

In server side, I done some proccess on files.

$imagesUrl = $this->uploadImages($request->file('files'));

dd($imagesUrl):

array:2 [▼
  "images" => array:4 [▼
    "original" => "/upload/images/2017/10453717_202487510125261_45876946_n.jpg"
    300 => "/upload/images/2017/300_10453717_202487510125261_45876946_n.jpg"
    600 => "/upload/images/2017/600_10453717_202487510125261_45876946_n.jpg"
    900 => "/upload/images/2017/900_10453717_202487510125261_45876946_n.jpg"
  ]

  "thumb" => "/upload/images/2017/300_10453717_202487510125261_45876946_n.jpg"
]

Now I want replace $imagesUrl with $request->file('files') and insert new record in database. I try this:

auth()->user()->article()->create(array_merge($request->all() , [ 'files' => $imagesUrl]));

But I get this error:

(1/1) ErrorException Array to string conversion

what is my wrong?

You should try this:

$imagesUrl = serialize($imagesUrl);

$arrInsert = ['title'=> $request->title,'files'=>$imagesUrl];

auth()->user()->article()->create($arrInsert);

and retrieve it again and then unserialize

$imagesUrl = unserialize($raw->files);

Answer: I added this code in modal

protected $casts = [
  'files' => 'array'
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM