Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何在larave(maatwebsite)中导出标题为的数据?_Excel_Laravel - Fatal编程技术网

Excel 如何在larave(maatwebsite)中导出标题为的数据?

Excel 如何在larave(maatwebsite)中导出标题为的数据?,excel,laravel,Excel,Laravel,因此,我使用此命令导出单个产品的详细信息。在导出时,我也想导出标题。那么我该如何导出数据和数据头呢 composer require maatwebsite/excel 在产品中(index.blade.php) 在ProductExport.php中 namespace App\Exports; use App\Product; use Maatwebsite\Excel\Concerns\FromCollection; class ProductExport implements F

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

composer require maatwebsite/excel
在产品中(index.blade.php)

在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'
          ]
    }
}

您需要将标题为的
添加到类中:

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'
          ]
    }
}

您需要将标题为的
添加到类中:

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'
          ]
    }
}

请向我们展示整个
ProductExport
类别代码。@ZakariaAcharki是的,我已经展示了我的完整代码请向我们展示整个
ProductExport
类别代码。@ZakariaAcharki是的,我已经展示了我的完整代码,还有?为什么?public function headings()…它显示必须将“headings”添加到导出数据中的错误。这不是你需要的吗?如果你不介意的话。你能给我一个小的exmaple吗?代码更新了,请检查。是的,我不知道为什么它在公共函数标题(){}上显示错误。它说返回类型声明必须与withHeadings()->headings()兼容:arrayand?为什么?public function headings()…它显示必须将“headings”添加到导出数据中的错误。这不是你需要的吗?如果你不介意的话。你能给我一个小的exmaple吗?代码更新了,请检查。是的,我不知道为什么它在公共函数标题(){}上显示错误。它说返回类型声明必须与withHeadings()->headings():数组兼容
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'
          ]
    }
}