I have Laravel project from Github, I am trying to export the selected column to a txt file. The table name is customers
and the column name is customer_contact_numbers
from the database khanoilsdb
Here is the error I am facing: I have added a new controller and the following code to it:
<?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');
And the button:
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="">Export</a>
</div>
What I am trying to do is, after users click on it, it will call the controller method and start downloading a txt file.
Sorry if I'm not able to explain properly, I'm new to Laravel and PHP. Thanks :)
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire