Reputation: 1276
I recently just got stuck with this problem. Exporting my database data to excel using Laravel Excel. I just copied the code on the tutorial links, but unfortunately it doesn't work on me. And I think I got everything setted up for laravel excel configuration.Can you help me solve this? Thanks. Here are my codes.
my controller method
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');
}
My Model
<?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'];
}
My Database Table
Upvotes: 0
Views: 841
Reputation: 38
Change your code : $products = Product::all(); to $products = Product::all()->toArray();
Upvotes: 1