<i id='Zobn7'><tr id='Zobn7'><dt id='Zobn7'><q id='Zobn7'><span id='Zobn7'><b id='Zobn7'><form id='Zobn7'><ins id='Zobn7'></ins><ul id='Zobn7'></ul><sub id='Zobn7'></sub></form><legend id='Zobn7'></legend><bdo id='Zobn7'><pre id='Zobn7'><center id='Zobn7'></center></pre></bdo></b><th id='Zobn7'></th></span></q></dt></tr></i><div id='Zobn7'><tfoot id='Zobn7'></tfoot><dl id='Zobn7'><fieldset id='Zobn7'></fieldset></dl></div>
    1. <tfoot id='Zobn7'></tfoot>

      <small id='Zobn7'></small><noframes id='Zobn7'>

      <legend id='Zobn7'><style id='Zobn7'><dir id='Zobn7'><q id='Zobn7'></q></dir></style></legend>
      • <bdo id='Zobn7'></bdo><ul id='Zobn7'></ul>

      1. 使用 Laravel 将表格下载为 CSV

        Use Laravel to Download table as CSV(使用 Laravel 将表格下载为 CSV)
        <legend id='ahX7q'><style id='ahX7q'><dir id='ahX7q'><q id='ahX7q'></q></dir></style></legend>
          <i id='ahX7q'><tr id='ahX7q'><dt id='ahX7q'><q id='ahX7q'><span id='ahX7q'><b id='ahX7q'><form id='ahX7q'><ins id='ahX7q'></ins><ul id='ahX7q'></ul><sub id='ahX7q'></sub></form><legend id='ahX7q'></legend><bdo id='ahX7q'><pre id='ahX7q'><center id='ahX7q'></center></pre></bdo></b><th id='ahX7q'></th></span></q></dt></tr></i><div id='ahX7q'><tfoot id='ahX7q'></tfoot><dl id='ahX7q'><fieldset id='ahX7q'></fieldset></dl></div>
          <tfoot id='ahX7q'></tfoot>
            <tbody id='ahX7q'></tbody>

              • <bdo id='ahX7q'></bdo><ul id='ahX7q'></ul>

                  <small id='ahX7q'></small><noframes id='ahX7q'>

                  本文介绍了使用 Laravel 将表格下载为 CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Laravel 作为 csv 文件导出数据库表.我希望用户能够选择 Export as CSV 按钮并将表格下载为 csv 文件.目前我已经得到了这个代码,但它不起作用:

                  I am trying to export a database table using Laravel as a csv file. I would like the user to be able to select the Export as CSV button and download the table as a csv file. Currently I've gotten this code but It is not working:

                  我的按钮:

                  <a href="/all-tweets-csv" class="btn btn-primary">Export as CSV</a>
                  

                  我的路线:

                  Route::get('/all-tweets-csv', function(){
                  
                      $table = Tweet::all();
                      $filename = "tweets.csv";
                      $handle = fopen($filename, 'w+');
                      fputcsv($handle, array('tweet text', 'screen name', 'name', 'created at'));
                  
                      foreach($table as $row) {
                          fputcsv($handle, array($row['tweet_text'], $row['screen_name'], $row['name'], $row['created_at']));
                      }
                  
                      fclose($handle);
                  
                      $headers = array(
                          'Content-Type' => 'text/csv',
                      );
                  
                      return Response::download($handle, 'tweets.csv', $headers);
                  });
                  

                  它返回给我这个错误:

                   The file "Resource id #154" does not exist
                  

                  我发现这是因为它正在尝试下载一个不存在的文件.是否有另一种方法可以修改我的代码以便下载为 csv.

                  And I've gathered that it is because it is trying to download a file that does not exist. Is there an alternative way I can go about modifying my code in order to download as a csv.

                  推荐答案

                  除了这一行,几乎一切都好:

                  Almost everything is fine except this line:

                  return Response::download($handle, 'tweets.csv', $headers);
                  

                  您应该将此行更改为:

                  return Response::download($filename, 'tweets.csv', $headers);
                  

                  这篇关于使用 Laravel 将表格下载为 CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  • <legend id='elaf4'><style id='elaf4'><dir id='elaf4'><q id='elaf4'></q></dir></style></legend>

                    <tfoot id='elaf4'></tfoot>
                    <i id='elaf4'><tr id='elaf4'><dt id='elaf4'><q id='elaf4'><span id='elaf4'><b id='elaf4'><form id='elaf4'><ins id='elaf4'></ins><ul id='elaf4'></ul><sub id='elaf4'></sub></form><legend id='elaf4'></legend><bdo id='elaf4'><pre id='elaf4'><center id='elaf4'></center></pre></bdo></b><th id='elaf4'></th></span></q></dt></tr></i><div id='elaf4'><tfoot id='elaf4'></tfoot><dl id='elaf4'><fieldset id='elaf4'></fieldset></dl></div>

                          <bdo id='elaf4'></bdo><ul id='elaf4'></ul>

                              <tbody id='elaf4'></tbody>

                            <small id='elaf4'></small><noframes id='elaf4'>