将自定义列添加到laravel excel

maf*_*tis 6 php excel laravel laravel-excel

我正在使用maatwebsite/excel,我想知道当我将数据导出为CSV时是否可以添加自定义列?

说明

我成功导出了我的products数据,但我的产品还有其他选项没有存储在我的产品表中,例如:specification.

我的规范存储在两个不同的表中specifications,这些表名为parent like CPU,subscpecifications其中child的存储位置如下:Core i5.

我用来存储孩子的id和产品id的另一个表,以便将每个产品与他们的子规范相关联.

声音完成了吗?:) here i provide ugly map to get the logic:)

屏幕1

现在,我尝试做的是:

在我的csv文件中添加额外的列,并包含每个产品的所有规格.

样品:

样品

代码

这是我目前的出口功能

public function export(Request $request) {
      $input = $request->except('_token');
      foreach ($input['cb'] as $key => $value) {
        if ($value== 'on') {
          $getRealInput[$key] = $input['defaultname'][$key];
        }
      }

      $products = Product::select($getRealInput)->get();


      Excel::create('products', function($excel) use($products, $request) {
        $excel->sheet('sheet 1', function($sheet) use($products, $request){

          $input = $request->except('_token');
          foreach ($input['cb'] as $key => $value) {
            if ($value== 'on') {
              $getCustomInput[$key] = $input['customname'][$key];
            }
          }

          $sheet->fromArray($products, null, 'A1', false, false);
          $sheet->row(1, $getCustomInput);
        });
      })->export('csv');
      return redirect()->back();
    }
Run Code Online (Sandbox Code Playgroud)

问题

  1. 那可能吗?
  2. 如果是,基于我上面的功能,我该怎么办?

提前致谢.

更新1

我已将此代码添加到我的函数中

$allRows = array();
  $data = array();
  foreach($products as $product){
  $specs = $product->subspecifications;
  foreach($specs as $spec){
    $data[] = $spec->specification->title;
    $data[] = $spec->title;
  }
}
array_push($allRows , $data);
Run Code Online (Sandbox Code Playgroud)

并改变了这一行:

$sheet->fromArray($products, null, 'A1', false, false);
Run Code Online (Sandbox Code Playgroud)

$sheet->fromArray($allRows, null, 'A1', false, false);
Run Code Online (Sandbox Code Playgroud)

现在这就是我所拥有的:

SCREEN3

这是我目前的全部功能:

public function export(Request $request) {
      $input = $request->except('_token');
      foreach ($input['cb'] as $key => $value) {
        if ($value== 'on') {
          $getRealInput[$key] = $input['defaultname'][$key];
        }
      }

      $products = Product::select($getRealInput)->get();


      Excel::create('products', function($excel) use($products, $request) {
        $excel->sheet('sheet 1', function($sheet) use($products, $request){

          $input = $request->except('_token');
          foreach ($input['cb'] as $key => $value) {
            if ($value== 'on') {
              $getCustomInput[$key] = $input['customname'][$key];
            }
          }


          // test code of adding subspacifications
          $allRows = array();
          $data = array();
          foreach($products as $product){
              $specs = $product->subspecifications;
              foreach($specs as $spec){
                    $data[] = $spec->specification->title;
                    $data[] = $spec->title;
              }
          }
          array_push($allRows , $data);
          $sheet->fromArray($allRows, null, 'A1', false, false);
          //
          // $sheet->fromArray($products, null, 'A1', false, false);
          $sheet->row(1, $getCustomInput);
        });
      })->export('csv');
      return redirect()->back();
    }
Run Code Online (Sandbox Code Playgroud)

更新2

那么今晚我玩了很多我的代码,最后:)我得到了我需要的,这是如何:

//codes...

// Here is you custom columnn logic goes
          foreach($products as $product){
            $specifications = DB::table('products')
            ->where('products.id', $product->id)
            ->join('product_subspecification', 'product_subspecification.product_id', '=', 'products.id')
            ->join('subspecifications', 'subspecifications.id', '=', 'product_subspecification.subspecification_id')
            ->select('subspecifications.title')
            ->pluck('title');

            $product['specifications'] = rtrim($specifications,',');
          }
          //


          $sheet->fromArray($products, null, 'A1', false, false);
          $sheet->row(1, $getCustomInput);

//... rest of the codes
Run Code Online (Sandbox Code Playgroud)

这将给我我的产品规格,但有3个小问题:

  1. 我没有在CSV文件中标注我的规范
  2. 没有规格的产品显示[]而不是什么
  3. 符合规格的产品也包括[]""

这里我提供了截图以便更好地理解:

screen5

Sha*_*ale 0

是的,这是可能的。为行创建数组,即。data = array(); 将单元格数据推送到数组

您也可以使用 eloquent 或 join 获取关系数据,这里我在循环内获取。

更新后的功能如下:

我试图与你的数据结构匹配

  public function export(Request $request) {
  $input = $request->except('_token');
  foreach ($input['cb'] as $key => $value) {
    if ($value== 'on') {
      $getRealInput[$key] = $input['defaultname'][$key];
    }
  }

  $products = Product::select($getRealInput)->get();


  Excel::create('products', function($excel) use($products, $request) {
    $excel->sheet('sheet 1', function($sheet) use($products, $request){


      // test code of adding subspacifications
      $allRows = array();
      array_push($allRows , ['id', 'title', 'specifications']); // Added title row
      $data = array();
      foreach($products as $product){
          $data[] = $product->id;    // Added product fields 
          $data[] = $product->title;
          $specs = $product->subspecifications;
          $spec_details = "";
          foreach($specs as $spec){                    
                $spec_details .= $spec->specification->title.':'. $spec->title. ' '; // appended specification:subspecification 
          }
          $data[] = $spec_details;
      }
      array_push($allRows , $data);
      $sheet->fromArray($allRows, null, 'A1', false, false);
      //
      // $sheet->fromArray($products, null, 'A1', false, false);
      //$sheet->row(1, $getCustomInput);   // commented
    });
  })->export('csv');
  return redirect()->back();
}
Run Code Online (Sandbox Code Playgroud)