通过路由从laravel中的mysql导出列时出错

6tr1vspr  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(291)

我有来自github的laravel项目,我正在尝试将所选列导出到一个txt文件。表名为 customers 列名称为 customer_contact_numbers 从数据库 khanoilsdb 我面临的错误是:

我添加了一个新控制器和以下代码:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CNController extends Controller
{
    $customers = Customer::all();
$phoneNumbers = "Phone numbers \n";
foreach ($customers as  $customer) {
  $content .= $customer->customer_contact_numbers;
  $content .= "\n";
}

// file name to download
$fileName = "contact_numbers.txt";

// make a response, with the content, a 200 response code and the headers
return Response::make($content, 200, [
  'Content-type' => 'text/plain', 
  'Content-Disposition' => sprintf('attachment; filename="%s"', $fileName),
  'Content-Length' => sizeof($content)
];);
}

web.php:

Route::get('/home', 'CNController');

和按钮:

<div class="dropdown-menu dropdown-menu-right">
    <a class="dropdown-item" href="{{ route('CNController') }}">Export</a>
  </div>


我想做的是,在用户点击它之后,它会调用controller方法并开始下载一个txt文件。
抱歉,如果我不能正确地解释,我是新的拉威尔和php。谢谢:)

fiei3ece

fiei3ece1#

问题是你的类中没有函数。
可以添加新函数 exportNumbers 这样地:

class CNController {
    public function exportNumbers() {
      // Your code here
    }
}

然后你必须编辑 web.php 要调用此函数的文件:

Route::get('/home', 'CNController@exportNumbers');

相关问题