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

tavares's avatar

Laravel Excel export

Hi, I'm using Laravel Excel from maatwebsite but i have a problem with export data to xls file. I have this query but i don't need to show all columns in the xls file but i need to select all this columns to do operations before download the file. In my select i have 8 columns but in my headers i just have 7 to show but this doesn't work because the 8ª column appears too.

MY FUNCTION: public function toExcel($id) { $b = Budget::find($id);

    $budget = Budget_Item::join('budgets', 'budget__items.id_budget', '=', 'budgets.id')
        ->join('items_sizes', 'budget__items.id_itemSize', '=', 'items_sizes.id')
        ->join('items', 'items_sizes.id_item', '=', 'items.id')
        ->join('material_types', 'items_sizes.id_materialType', '=', 'material_types.id')
        ->select('items.reference AS Referência', 'items.name AS Descrição', 'items_sizes.size AS Tamanho', 'material_types.material_type AS Material', 'budget__items.amount AS Quantidade', 'items_sizes.unit_price AS Val.Unitário', 'budget__items.price AS Val.Total', 'budget__items.purchasePrice')
        ->where('id_budget', '=', $id)
        ->get();

    $budgetUpdate = [];
    $budgetUpdate[] = ['Referência', 'Descrição', 'Tamanho', 'Material', 'Quantidade', 'Val.Unitário', 'Val.Total'];
    foreach ($budget as $key)
    {
      if ($key->purchasePrice > 0)
      {
        $key->unit_price = $key->purchasePrice;
      }

      $budgetUpdated[] = $key->toArray();
    }
    Excel::create('Proposta_'.$b->reference, function($excel) use($budgetUpdated)
    {
      // Set the title
      $excel->setTitle('Proposta');

      $excel->sheet('Sheetname', function($sheet) use($budgetUpdated)
      {
        $sheet->fromArray($budgetUpdated, null, 'A1', false, true);
      });

    })->download('xls');

}

How can i do that?

Thank's

0 likes
0 replies

Please or to participate in this conversation.