0

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

enter image description here

Additional Information

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,

sss

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.

UPDATE

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"
]

Questions

  1. How to I get to edit this column names before export my file?
  2. How do I get checkbox for each column? (explanation below)

Checkbox

I need checkbox for each column to say this column be included export file or not.

example I will uncheck created_at column so it will not be included my exported CSV file.

UPDATE 2

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

sdd

Codes

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?

mafortis
  • 6,750
  • 23
  • 130
  • 288

1 Answers1

1

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');
}
pyy
  • 893
  • 1
  • 8
  • 22
  • Can I? I don't know if I can or not but that's what I want to do, first edit then export. `important part is` to be able to type my custom names and not to define them in controller, just like image at the bottom of my question. – mafortis Mar 26 '18 at 09:34
  • @mafortis You should be able to. Can you make sure the above code works, if it does then you'll be able to build your query into the data you need dynamically. Could you also show an example of the form the user will submit to specify field names. – pyy Mar 26 '18 at 09:43
  • Currently I don't have any form in my panel only simple export button which is working by my function `provided in question`, what i want to be happen is like this: `1-click export button`->`2-bootstrap modal will open including all my products columns (columns not their rows)`->`3-have some checkbox to select what columns be included export file`->`4-i can change or keep my column names as they are`->`5-click save button` – mafortis Mar 26 '18 at 09:48
  • base on your update, what should i loop in my panel in order to get my column names for edit? `$list ` or `$products` ? `second question` what about check boxes? – mafortis Mar 26 '18 at 09:59
  • Neither.. You need to print each column in the table, as shown in your last screenshot. What about check boxes? – pyy Mar 26 '18 at 10:07
  • I don't get it! so how can i have my column names for edit before export? `checkboxes` as i mentioned here https://stackoverflow.com/questions/49487888/laravel-custom-csv-exporting#comment85979578_49488071 in `no.3` + ability of editing column names before export I also need checkbox for each column to say this column be included export file or not. `example` i will uncheck `created_at` column so it will not be included my exported csv file then. – mafortis Mar 26 '18 at 10:11
  • For no.3, when looping around the columns you pass back have a check for whatever you call the checkbox, if it's checked add that row to $list, if it's unchecked, don't. You can get your column names for the model of the table I'd imagine. – pyy Mar 26 '18 at 10:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167566/discussion-between-pyy-and-mafortis). – pyy Mar 26 '18 at 13:43