如何在larave(maatwebsite)中导出带有其标头的数据?

初学者编码器

因此,我正在使用此命令导出我的单个产品详细信息。在导出时,我也要导出标题。那么我如何导出数据和数据头呢?

composer require maatwebsite/excel

在产品中(index.blade.php)

@foreach($products as $product)
    <tr>{{$product->name}}</tr>
        <tr>{{$product->price}}</tr> 
        <tr><a href="{{route('prodcutExport',$product->id)}}">Export Product Detail</></tr>
@endforeach

在ProductController.php中

 public function export()
    {
    
    $products = Product::where('id',1)->get();    
    return Excel::download(new ProductExport($products), 'patient.xlsx');
   }

在ProductExport.php中


namespace App\Exports;

use App\Product;
use Maatwebsite\Excel\Concerns\FromCollection;

class ProductExport implements FromCollection
{
  
    public function __construct($products)
    {
        $this->products = $products;
     }


    public function collection() 
    {
        return $this->products;
    }
  
    public function headings()  //How can i export this
    {
        return [
          'Name','Price','Colour','Created At'
          ]
    }
}
扎卡里亚·阿查尔基(Zakaria Acharki)

您需要添加WithHeadings到您的班级:

namespace App\Exports;

use App\Product;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class ProductExport implements FromQuery, WithHeadings
{  
    public function __construct($products)
    {
        $this->products = $products;
     }


    public function collection() 
    {
        return $this->products;
    }
  
    public function headings(): array
    {
        return [
          'Name','Price','Colour','Created At'
          ]
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 SQL Server 中导出带有图表的数据库?

如何在Octave中导出带有索引的矩阵?

如何在Google Dataprep中导出带有标题的文件?

如何在Maatwebsite中获取Excel标头和标题?

如何在Python中将带有永久标头的数据写入文件?

如何在带有pyhton中的标头的数据框中转换汇总输出?

如何在Swift框架中导入私有框架标头?

如何在ics文件中导出数据?

如何在R中导出摘要数据?

如何在终端的路径中导出带有空格的环境变量

如何在QBO Canada中导出带有税码和值的行项目

如何在Confluence REST API中导出带有注释的空间?

如何在搅拌机中导出带有纹理的.obj格式文件?

当没有固定标头时如何在熊猫中将数据与标头映射

如何在jq中向CSV导出添加标头?

邮递员-如何查看带有标头和正文数据并替换了变量的请求

Node.js:如何使用请求模块发送带有表单数据的标头?

如何发送带有数据的http标头以休息Api codiegniter?

如何在Spring Boot中读取带有标头和正文的JSON

如何在Windows上使用带有mingw-w64标头的clang

如何在Swagger UI中发送带有请求的自定义标头?

如何在React Native应用程序中获取带有标头的api(POST)

如何在带有预编译标头的项目中使用google protobuf

如何在带有参数和标头的Android Volley库中使用POST请求?

如何在 Angular 7 中的 get 请求中发送带有标头的 apikey

如何在Flutter中滚动带有粘性标头的堆叠式容器?

如何在Apache Drill中读取带有标头的文件

如何在Laravel中导出具有日期范围的MYSQL数据?

如何在数据表的Excel导出文件中添加空白行或自定义标头?