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条回答 默认 最新

相关推荐 更多相似问题