在使用 maatwebsite laravel excel 3.1 導(dǎo)出數(shù)據(jù)時(shí),query() 方法期望返回一個(gè) eloquent 查詢構(gòu)建器實(shí)例,而不是直接執(zhí)行查詢并返回結(jié)果集。當(dāng)你的導(dǎo)出需要涉及復(fù)雜的 join 操作或者需要使用 db facade 時(shí),需要進(jìn)行一些調(diào)整。
理解 query() 方法的正確用法
query() 方法的核心在于返回一個(gè)可以被 Laravel Excel 處理的查詢構(gòu)建器對(duì)象。這意味著你需要將你的 DB::table() 查詢構(gòu)建器傳遞給 Laravel Excel,而不是執(zhí)行 get() 方法。
示例:使用 DB facade 和 JOIN 構(gòu)建查詢
假設(shè)你有一個(gè)如下的查詢,它使用了 DB::table() 和 JOIN:
public function query() { return DB::table('complaints') ->join('customers', 'customers.id', '=', 'complaints.customer_id') ->join('technicians', 'technicians.id', '=', 'complaints.technician_id') ->select( 'complaints.*', 'customers.first_name', 'customers.last_name', 'customers.number', 'customers.email_address', 'customers.occupancy_name', 'customers.building_number', 'customers.house_number', 'customers.taluka', 'customers.city', 'customers.address', 'customers.package', 'customers.package_date', 'customers.package_status', 'customers.package_period', 'technicians.first_name as t_fname', 'technicians.last_name as t_lname', 'technicians.number as t_number' ) ->where(function ($query) { $query->where('customers.package', '=', "Platinum") ->where(DB::raw('timediff(now(), complaints.date )'), '>', '06:00:00') ->where('complaints.status', '=', "Assigned"); }) ->orWhere(function ($query) { $query->where('customers.package', '=', "Gold") ->where(DB::raw('timediff(now(), complaints.date )'), '>', '24:00:00') ->where('complaints.status', '=', "Assigned"); }) ->orWhere(function ($query) { $query->where('customers.package', '=', "Silver") ->where(DB::raw('timediff(now(), complaints.date )'), '>', '48:00:00') ->where('complaints.status', '=', "Assigned"); }); }
關(guān)鍵點(diǎn):移除 get() 方法
不要在 query() 方法中調(diào)用 get()。Laravel Excel 會(huì)自動(dòng)處理查詢的執(zhí)行和數(shù)據(jù)的獲取。你只需要返回查詢構(gòu)建器對(duì)象即可。
確保查詢返回?cái)?shù)據(jù)
如果導(dǎo)出的 Excel 文件為空,首先要確認(rèn)的是你的查詢本身是否返回任何數(shù)據(jù)。可以在 SQL IDE 中執(zhí)行相同的查詢,或者在 Laravel 中使用 dd() 函數(shù)來調(diào)試查詢結(jié)果:
public function query() { $query = DB::table('complaints') ->join('customers', 'customers.id', '=', 'complaints.customer_id') ->join('technicians', 'technicians.id', '=', 'complaints.technician_id') ->select( 'complaints.*', 'customers.first_name', 'customers.last_name', 'customers.number', 'customers.email_address', 'customers.occupancy_name', 'customers.building_number', 'customers.house_number', 'customers.taluka', 'customers.city', 'customers.address', 'customers.package', 'customers.package_date', 'customers.package_status', 'customers.package_period', 'technicians.first_name as t_fname', 'technicians.last_name as t_lname', 'technicians.number as t_number' ) ->where(function ($query) { $query->where('customers.package', '=', "Platinum") ->where(DB::raw('timediff(now(), complaints.date )'), '>', '06:00:00') ->where('complaints.status', '=', "Assigned"); }) ->orWhere(function ($query) { $query->where('customers.package', '=', "Gold") ->where(DB::raw('timediff(now(), complaints.date )'), '>', '24:00:00') ->where('complaints.status', '=', "Assigned"); }) ->orWhere(function ($query) { $query->where('customers.package', '=', "Silver") ->where(DB::raw('timediff(now(), complaints.date )'), '>', '48:00:00') ->where('complaints.status', '=', "Assigned"); }); dd($query->get()); // 調(diào)試查詢結(jié)果 return $query; }
如果 dd($query->get()) 返回空集合,那么問題出在你的查詢條件上,需要仔細(xì)檢查 WHERE 子句。
注意事項(xiàng)
總結(jié)
在使用 Laravel Excel 3.1 導(dǎo)出數(shù)據(jù)時(shí),特別是涉及到復(fù)雜的查詢和 JOIN 操作時(shí),關(guān)鍵在于 query() 方法返回的是查詢構(gòu)建器對(duì)象,而不是查詢結(jié)果集。移除 get() 方法,并確保你的查詢本身能夠返回有效的數(shù)據(jù)。通過調(diào)試查詢結(jié)果,可以快速定位問題所在,并確保導(dǎo)出的 Excel 文件包含所需的數(shù)據(jù)。
以上就是Laravel Excel 3.1:使用查詢構(gòu)建器導(dǎo)出包含 JOIN 的數(shù)據(jù)的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
每個(gè)人都需要一臺(tái)速度更快、更穩(wěn)定的 PC。隨著時(shí)間的推移,垃圾文件、舊注冊(cè)表數(shù)據(jù)和不必要的后臺(tái)進(jìn)程會(huì)占用資源并降低性能。幸運(yùn)的是,許多工具可以讓 Windows 保持平穩(wěn)運(yùn)行。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.400tele.com.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)