duanming7833 2019-03-01 01:56
浏览 21
已采纳

将一个功能拆分为两个

for the reusability, I plan to split my function into two functions. Basically, the concept of the function is loads the input file then preview the data.

Working Code

public function uploadImportCsv()
    {
        $file = Input::file('file');
        $extension = $file->getClientOriginalExtension();
        $filename = sha1($file->getClientOriginalName().time()) . ".{$extension}";
        //upload to s3
        #doing upload to s3

        $data = [
            'title'=>[],
            'value'=>[]
            ];
    $results = Excel::load(Input::file('file'), function($reader){

            })->get();
    foreach ($results as $result) {
        foreach ($result as $key => $value) {
            if(!in_array($key, $data['title'])){
                array_push($data['title'], $key);
            }       
        }
        array_push($data['value'], $result);
    }

        return Response::json(['filename' => $filename, 'data' => $data]);
    }

after split

public function previewCsv()
    {
        //Preview table
        $data = [
                'title'=>[],
                'value'=>[]
                ];
        $results = Excel::load(Input::file('file'), function($reader){

                })->get();
        foreach ($results as $result) {
            foreach ($result as $key => $value) {
                if(!in_array($key, $data['title'])){
                    array_push($data['title'], $key);
                }       
            }
            array_push($data['value'], $result);
        }
        return Response::json(['data' => $data]);
    }

    public function uploadImportCsv()
    {
        $file = Input::file('file');
        $extension = $file->getClientOriginalExtension();
        $filename = sha1($file->getClientOriginalName().time()) . ".{$extension}";
        //upload to s3
        #doing upload to s3

        $data = $this->previewCsv();


        return Response::json(['filename' => $filename,'data' => $data]);
    }

I called the function from the preview function but it does not work.

  • 写回答

1条回答 默认 最新

  • doutang7415 2019-03-01 02:33
    关注

    If you need same result, it's not necessary to return a json object from previewCsv funtion.

    public function previewCsv()
    {
        ....
        return $data;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题