Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fjo24's avatar
Level 1

i need to filter cells to export in excel sheet with maatwebsite/excel

Hi.. i am using this code:

in the model:

public function scopeSearch($query, $date){
        $marca= $date->idmarca;
        $placa= $date->placa;
        $modelo= $date->idmodelo;
        $año1=$date->año1;
        $año2=$date->año2;
        $combustion_gas= $date->combustion_gas;
        $combustion_glp= $date->combustion_glp;
        $combustion_gnv= $date->combustion_gnv;
        $combustion_petroleo= $date->combustion_petroleo;
        $proxima_visita1=$date->proxima_visita1;
        $proxima_visita2=$date->proxima_visita2;
        $no_atender= $date->no_atender;

        if($combustion_gas=='1'){
            $gas=$combustion_gas;
        }else{
            $gas=null;
        }

 
        if($combustion_glp=='1'){
            $glp=$combustion_glp;
        }else{
            $glp=null;
        }
        if($combustion_gnv=='1'){
            $gnv=$combustion_gnv;
        }else{
            $gnv=null;
        }
        if($combustion_petroleo=='1'){
            $petroleo=$combustion_petroleo;
        }else{
            $petroleo=null;
        }
        if($no_atender=='1'){
            $atendido=$no_atender;
        }else{
            $atendido=null;
        }
        if ($año1=="") {
            $año1=null;
        } else {
            $año1=\Carbon\Carbon::create($date['año1'])->startOfYear()->format('Y-m-d');
        }
        if ($año2=="") {
            $año2=null;
        } else {
            $año2=\Carbon\Carbon::create($date['año2'])->endOfYear()->format('Y-m-d');
        }
        if ($proxima_visita1=='') {
            $proxima_visita1=null;
        } else {
            $proxima_visita1=\Carbon\Carbon::parse($date['proxima_visita1'])->format('Y-m-d');
        }
        if ($proxima_visita2=='') {
            $proxima_visita2=null;
        } else {
            $proxima_visita2=\Carbon\Carbon::parse($date['proxima_visita2'])->format('Y-m-d');
        }
        if (($año1 != "")&&($año2 != ""))
        {
            $query->whereBetween('año', [$año1, $año2]);
        }elseif($año1 != ""){
            $query->where('año', '>=', $año1);
        }elseif($año2 != ""){
            $query->where('año', '<=', $año2);
        }
        if (($proxima_visita1 != "")&&($proxima_visita2 != ""))
        {
            $query->whereBetween('proxima_visita', [$proxima_visita1, $proxima_visita2]);
        }elseif($proxima_visita1 != ""){
            $query->where('proxima_visita', '>=', $proxima_visita1);
        }elseif($proxima_visita2 != ""){
            $query->where('proxima_visita', '<=', $proxima_visita2);
        }
        return $query
            ->join('marca', 'marca.idmarca', '=', 'vehiculo.idmarca')
            ->join('modelo', 'modelo.idmodelo', '=', 'vehiculo.idmodelo')
**          ->select('vehiculo.id', 'vehiculo.placa','vehiculo.idmarca', 'marca.nombre as Marca', 'vehiculo.idmodelo', 'modelo.nombre as Modelo', 'vehiculo.combustion_gas', 'vehiculo.combustion_glp', 'vehiculo.combustion_gnv', 'vehiculo.combustion_petroleo', 'vehiculo.num_motor', 'vehiculo.km', 'vehiculo.proxima_visita', 'vehiculo.no_atender', 'vehiculo.motivo_no_atencion')                                                      **
            ->where('vehiculo.idmarca', 'LIKE', "%$marca%")
            ->where('vehiculo.idmodelo', 'LIKE', "%$modelo%")
            ->where('vehiculo.combustion_gas', 'LIKE', "%$gas%")
            ->where('vehiculo.combustion_glp', 'LIKE', "%$glp%")
            ->where('vehiculo.combustion_gnv', 'LIKE', "%$gnv%")
            ->where('vehiculo.combustion_petroleo', 'LIKE', "%$petroleo%")->where('vehiculo.no_atender', 'LIKE', "%$atendido%");
    }

this code print all fields in select. I want to discard the ids, the problem is that the $query variable needs those fields, to get the name of the makes and models, and the other id of vehicle for a function in the view. i continue...

**code of the controller: **

public function query(Request $request)
    {
        $vehiculos = Vehiculo::search($request)->orderBy('placa', 'ASC')->get('id');
        Excel::create('Lista de vehiculos consultados', function ($excel) use($vehiculos) {
            $excel->sheet('Listado', function ($sheet) use ($vehiculos) {

                $sheet->fromArray($vehiculos);
            });
        })->store('xls', storage_path('excel/exports/'.Auth()->user()->id.'/'));
        return view('asesor.vehiculo.query')->with('vehiculos', $vehiculos);
    }

    public function exportquery()
    {
        return response()->download(storage_path('excel/exports/'.Auth()->user()->id.'/Lista de vehiculos consultados.xls'));
    }

i think that... I should filter in the controller, the variable $vehicles receives from the $query variable the data, since it calls the scopeSearch, I need to apply a select there to send it to excel ... Can someone tell me how? Note: excel works fine, print everything. I just want to select the fields that should come out in excel ...

thanks a lot!

0 likes
0 replies

Please or to participate in this conversation.