0

我最近刚刚被这个问题困住了。使用 Laravel Excel 将我的数据库数据导出到 Excel。我刚刚复制了教程链接上的代码,但不幸的是它对我不起作用。而且我想我已经为 laravel excel 配置设置了一切。你能帮我解决这个问题吗?谢谢。这是我的代码。

我的控制器方法

public function exportInventory(){

    $products = Product::all();

    Excel::create('products', function($excel) use($products){

    $excel->sheet('Excel sheet', function($sheet) use($products){

      $sheet->fromArray($products);
      $sheet->setOrientation('landscape');

    });

    })->export('xls');

}

我的模特

<?php

 namespace App\Product;

 use Illuminate\Database\Eloquent\Model;

 class Product extends Model
 {

//
   protected $fillable =   ['pharmaceutical','description','unit','quantity','price','amount','type','packaging','lot','expiry_date_month'];
   protected $guarded = ['price'];
 }

我的数据库表

在此处输入图像描述

错误 在此处输入图像描述

4

1 回答 1

1

更改您的代码: $products = Product::all(); to $products = Product::all()->toArray();

于 2016-04-09T12:05:28.797 回答