php - Laravel custom CSV exporting


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 hastitledescriptioncreated_atupdated_at what I want is to export something liketitlebody as you see here i removedcreated_at ,updated_at and also renamed mydescription column tobody 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){

PS: what I try to create is exactly what this extension does in Magento,


As you can see in this extension we are able tochange 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


and mycontroller is like:

public function export(Request $request) {
      $product = new Product;
      $list = $product->getTableColumns();
      //$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"


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


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.


I edited5 columns of my database and I got10



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);
      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'


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){

People are also looking for solutions to the problem: php - Select last inserted data from table


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.