我試圖通過連接三個表使Laravel中的Excel工作表在Excel工作表中輸出查詢的結果。class stdClass的對象無法轉換爲Excel表格中的Laravel字符串下載
型號檢索查詢結果:
<?php
namespace App;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class Product extends Model {
protected $table= 'product_master';
protected $primaryKey = 'product_id';
public static function download() {
$data=DB::table('product_master')
->join('subcategory_master', 'product_master.subcategory_id', '=', 'subcategory_master.id')
->join('category_master', 'product_master.category_id', '=', 'category_master.id')
->select('product_master.product_id','product_master.product_name','product_master.part_number','category_master.category_name','subcategory_master.subcategory_name','product_master.net_quandity','gross_weight','product_master.product_type','product_master.description')
->where('delete_status','=','0')->get();
return($data);
}
}
控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Product;
use Excel;
class ExcelController extends Controller {
public function downloadExcel(Request $request, $type) {
$data = Product::download()->all();
return Excel::create('productmaster', function($excel) use ($data) {
$excel->sheet('mySheet', function($sheet) use ($data) {
$sheet->fromarray($data);
});
})->download($type);
}
}
路線:
Route::get('downloadExcel/{type}', '[email protected]');
究竟是在哪裏發生的錯誤? – Kai
$ sheet-> fromArray($ data);在控制器 –