doulaobi7988 2018-09-03 15: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 15: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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败