【问题标题】:how to export multiple sheets from multiples querys whit Laravel Excel?如何使用 Laravel Excel 从多个查询中导出多个工作表?
【发布时间】:2021-10-21 16:40:24
【问题描述】:

我需要在 Laravel 8 的 excel 中创建一个报表,有几个特定的​​查询,每个查询都保存在不同的 excel 表中,laravel excel 文档没有解释。

有谁知道这样做的结构是怎样的?

我有这样的疑问:

$cursosvigentes = Reporte::selectRaw('concat(users.firstSurname, " ", users.secondSurname) AS fullSurname, concat(users.firstName," ", users.secondName) AS fullName, users.documento, capacitaciones.capacitacion, capacitaciones.fi, capacitaciones.fv, datediff(capacitaciones.fv, ?) AS diff',[$date])
        ->join('users','capacitaciones.id_usuario','=','users.documento')
        ->whereRaw(' datediff(capacitaciones.fv, ? ) < ?', [$date, 30])
        ->whereRaw(' datediff(capacitaciones.fv, ? ) > ?', [$date, 0])->get();

    $datostrabajadores = User::select('documento')->get();

【问题讨论】:

    标签: sql laravel laravel-8 export-to-excel laravel-excel


    【解决方案1】:

    向 Excel 类发送不同的参数。

    控制器:

    $customer = User::all();
    $filterCustomer = User::all()->where()->get();
    (new ExportExcel($customer))->download('users.xlsx');
    (new ExportExcel($filterCustomer))->download('users.xlsx');
    

    ExportExcel 类:

    class ExportExcel implements FromQuery
    {
        private $users;
        private $capacitaciones;
    
        public function __construct($users, $capacitaciones)
        {
            $this->users = $users;
            $this->capacitaciones= $capacitaciones;
    
        }
    
        public function query()
        {
            return ;  // Any query that includes these users
        }
    }
    

    【讨论】:

    • 嗨,我按照你的指示试过了,但我做不到,我有 2 个表名为:“capacitaciones”和“users”,我有 2 个查询:$sqlcapacitaciones 和 $sqlusers,模型“capacitaciones”称为“Reporte”,“users”模型称为“User”
    • 所以,将两个参数传递给构造函数...我编辑了我的答案
    猜你喜欢
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    相关资源
    最近更新 更多