doulaobi7988 2018-09-03 07:37
浏览 61
已采纳

Laravel处理复杂的请求

I have a request that sends a FormData to my API.

It has following structure:

image: file type image;
text: 'some long string';
array: ['string', 'string'];
data: [
  { field: 'string', link: 'url' },
  { field: 'string', link: 'url' },
]

I successfully handled first three inputs but stuck with the last one.

Laravel receives it as an array of strings instead of an array of objects.

I'm not able to access fields neither via $request->data->field nor via $request->data['field'}

Also I tried to use json_decode but this returns null

Update #1

array(4) {
  ["text"]=>
  string(17) "asdajsklfsdnfnads"
  ["array"]=>
  array(2) {
    [0]=>
    string(6) "sadasd"
    [1]=>
    string(6) "safsdf"
  }
  ["data"]=>
  array(2) {
    [0]=>
    string(39) "{link: "http://sdf.sdf", field: "asfd"}"
    [1]=>
    string(44) "{link: "http://sdf.sasdf", field: "asasdfd"}"
  }
  ["image"]=>
  object(Illuminate\Http\UploadedFile)#787 (7) {

展开全部

  • 写回答

1条回答 默认 最新

  • dsfjnxjlbqv9812 2018-09-03 07:59
    关注

    Try this code

    $result = [];
    foreach($request->data as $data) {
       $object = json_decode($data);
       $result[] = $object;
       // $object['filed'] you can access in field attribute 
    }
    
    dd($result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部