用新值替换数组中的键值

时间:2017-08-24 14:30:28

标签: php laravel laravel-5.4

我在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"
]

现在我想用$imagesUrl替换$request->file('files')并在数据库中插入新记录。 我试试这个:

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

但是我收到了这个错误:

  

(1/1)ErrorException数组到字符串转换

我错了什么?

2 个答案:

答案 0 :(得分:0)

你应该试试这个:

$imagesUrl = serialize($imagesUrl);

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

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

再次检索它然后反序列化

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

答案 1 :(得分:0)

答案:我在模态中添加了此代码

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