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?

1
  • I've found my solution no longer need this question. Commented Mar 29, 2018 at 7:42

1 Answer 1

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');
}
Sign up to request clarification or add additional context in comments.

8 Comments

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 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.
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
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?
Neither.. You need to print each column in the table, as shown in your last screenshot. What about check boxes?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.