
在使用 maatwebsite laravel excel 3.1 导出数据时,query() 方法期望返回一个 eloquent 查询构建器实例,而不是直接执行查询并返回结果集。当你的导出需要涉及复杂的 join 操作或者需要使用 db facade 时,需要进行一些调整。
理解 query() 方法的正确用法
query() 方法的核心在于返回一个可以被 Laravel Excel 处理的查询构建器对象。这意味着你需要将你的 DB::table() 查询构建器传递给 Laravel Excel,而不是执行 get() 方法。
示例:使用 DB facade 和 JOIN 构建查询
假设你有一个如下的查询,它使用了 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");
});
}关键点:移除 get() 方法
不要在 query() 方法中调用 get()。Laravel Excel 会自动处理查询的执行和数据的获取。你只需要返回查询构建器对象即可。
确保查询返回数据
如果导出的 Excel 文件为空,首先要确认的是你的查询本身是否返回任何数据。可以在 SQL IDE 中执行相同的查询,或者在 Laravel 中使用 dd() 函数来调试查询结果:
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()); // 调试查询结果
return $query;
}如果 dd($query->get()) 返回空集合,那么问题出在你的查询条件上,需要仔细检查 WHERE 子句。
注意事项
总结
在使用 Laravel Excel 3.1 导出数据时,特别是涉及到复杂的查询和 JOIN 操作时,关键在于 query() 方法返回的是查询构建器对象,而不是查询结果集。移除 get() 方法,并确保你的查询本身能够返回有效的数据。通过调试查询结果,可以快速定位问题所在,并确保导出的 Excel 文件包含所需的数据。
以上就是Laravel Excel 3.1:使用查询构建器导出包含 JOIN 的数据的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号