Laravel 将查询结果下载为 CSV

我正在尝试以 CSV 格式下载查询,目前面临两个问题:

  1. 在公用文件夹中创建一个文件。它包含查询数据。这真的很糟糕,因为它不应该存在于公用文件夹中。

  2. 还下载了一个文件,但下载的文件是空的。

这是功能:

public function get_chatmessages(Request $data) {


    try {  

        if ($data->chat_to_user) {              

            $result = DB::connection('mysql_live')->table('user_chatmessages')

                ->where(function($query) use($data) {

                    $query->where('from_user', $data->chat_from_user)->where('to_user', $data->chat_to_user);

                })->orWhere(function($query) use($data) {

                    $query->where('to_user', $data->chat_from_user)->where('from_user', $data->chat_to_user);

                })->orderBy('date_added', 'asc')->get();

        } else {

            $result = DB::connection('mysql_live')->table('user_chatmessages')

            ->where('from_user', $data->chat_from_user)

            ->orWhere('to_user', $data->chat_from_user)

            ->orderBy('date_added', 'asc')

            ->get();

        }


        //\Log::info($data);

        //\Log::info($result);


        $headers = array(

            "Content-type" => "text/csv",

            "Content-Disposition" => "attachment; filename=file.csv",

            "Pragma" => "no-cache",

            "Cache-Control" => "must-revalidate, post-check=0, pre-check=0",

            "Expires" => "0"

        );


        $columns = array('from_user', 'to_user', 'message', 'date_added');

        $callback = function() use ($result, $columns) {

            $file = fopen('output_chat.csv', 'w');

            fputcsv($file, $columns);


            foreach($result as $res) {

                fputcsv($file, array($res->from_user, $res->to_user, $res->message, $res->date_added));

            }

            fclose($file);

        };

    } catch (\Exception $e) {  

        return redirect('home')->with('error', $e->getMessage());  

    } 


    return redirect('home')->with('error', 'Etwas ist schief gelaufen');  

}


叮当猫咪
浏览 255回答 3
3回答

ABOUTYOU

我在你的片段中做了一些关于 php://output$headers = [&nbsp; &nbsp; "Content-type"&nbsp; &nbsp; &nbsp; &nbsp; => "text/csv",&nbsp; &nbsp; "Content-Disposition" => "attachment; filename=output_chat.csv", // <- name of file&nbsp; &nbsp; "Pragma"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => "no-cache",&nbsp; &nbsp; "Cache-Control"&nbsp; &nbsp; &nbsp; &nbsp;=> "must-revalidate, post-check=0, pre-check=0",&nbsp; &nbsp; "Expires"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> "0",];$columns&nbsp; = ['from_user', 'to_user', 'message', 'date_added'];$callback = function () use ($result, $columns) {&nbsp; &nbsp; $file = fopen('php://output', 'w'); //<-here. name of file is written in headers&nbsp; &nbsp; fputcsv($file, $columns);&nbsp; &nbsp; foreach ($result as $res) {&nbsp; &nbsp; &nbsp; &nbsp; fputcsv($file, [$res->from_user, $res->to_user, $res->message, $res->date_added]);&nbsp; &nbsp; }&nbsp; &nbsp; fclose($file);};

MMMHUHU

你可以决定保存文件的位置——你可以使用 Laravel 的 Storage 外观来做到这一点。private function pathToPrivateStorage(){&nbsp; &nbsp; return '/private/CSVs';}Storage::put($this->pathToPrivateStorage, $YOURCSVFILE);您可以在此处阅读有关存储的更多信息。

慕姐4208626

尝试$file = fopen('php://output', 'w');代替$file = fopen('output_chat.csv', 'w');
打开App,查看更多内容
随时随地看视频慕课网APP