I would like to know is it possible for me to edit columns before exporting my CSV file or not?
For example I have products table where has title
description
created_at
updated_at
what I want is to export something like title
body
as you see here i removed created_at
, updated_at
and also renamed my description
column to body
is that possible?
I am using this package version 2.1.0 Info Graph
currently I'm using default export code from samples, including no additional query etc. try to figure how should I change my code in order to export my data as I wish.
public function export() {
$products = Product::all();
Excel::create('products', function($excel) use($products) {
$excel->sheet('sheet 1', function($sheet) use($products){
$sheet->fromArray($products);
});
})->export('xls');
PS: what I try to create is exactly what this extension does in Magento,
As you can see in this extension we are able to change default database column names
, add/remove columns
etc. (but i only need this two options), before export the data.
I managed to get my table columns names like:
Product model
public function getTableColumns() {
return $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable());
}
and my controller
is like:
public function export(Request $request) {
$product = new Product;
$list = $product->getTableColumns();
dd($list);
//$products = Product::select($list)->get();
// Excel::create('products', function($excel) use($products) {
// $excel->sheet('sheet 1', function($sheet) use($products){
// $sheet->fromArray($products);
// });
// })->export('xls');
}
my list dd
array:27 [▼
0 => "id"
1 => "title"
2 => "slug"
3 => "imageOne"
4 => "imageTwo"
5 => "short_description"
6 => "description"
7 => "price"
8 => "meta_description"
9 => "meta_tags"
10 => "arrivalDays"
11 => "height"
12 => "weight"
13 => "lenght"
14 => "width"
15 => "sku"
16 => "stock"
17 => "label"
18 => "label_from"
19 => "label_to"
20 => "label_color"
21 => "status_id"
22 => "brand_id"
23 => "category_id"
24 => "subcategory_id"
25 => "created_at"
26 => "updated_at"
]
before export my file
?I need checkbox for each column to say this column be included export file or not.
example
I will uncheckcreated_at
column so it will not be included my exported CSV file.
Ok, I get it to work with my custom names. There is tiny issue:
My data row will repeat twice in excel file.
Explain
I edited 5
columns of my database and I got 10
This is my updated code:
public function export(Request $request) {
$products = Product::all();
Excel::create('products', function($excel) use($products, $request) {
$excel->sheet('sheet 1', function($sheet) use($products, $request){
$ddd = $request->except('_token');
foreach($ddd as $fff){
$ddd[] = $fff;
}
$sheet->fromArray($products, null, 'A1', false, false);
$sheet->row(1, $ddd);
});
})->export('csv');
return redirect()->back();
}
any idea?
Can you not transform the data before creating the excel sheet? For example:
$products = Product::select(
'subject as title',
'body as description'
)->get();
EDIT:
From your provided updates, it'd be something like this:
public function export(Request $request)
{
$list = [];
foreach($request->columns as $column){
$list[] = $column->DatabaseHeadingName . ' as ' . $column->CustomHeadingName;
}
$products = Product::select($list)->get();
Excel::create('products', function($excel) use($products) {
$excel->sheet('sheet 1', function($sheet) use($products){
$sheet->fromArray($products);
});
})->export('xls');
}