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

matthewmm's avatar

Customizing fields for export using Laravel Excel

Hi guys, This is the package I am currently using for my application. http://www.maatwebsite.nl/laravel-excel/docs/export

Currently in my database I have these columns (Date, Project, Ticket Number, Task, Time_in, Time_out, Created_at, updated_at) In this particular page called Timesheet, Only Date, Project, Ticket Number, Task and a calculation of time difference between time_in and time_out will be displayed under the "Hours" column.

Edit I got it to select the specific columns I want already but as the part in the blade view, where it is a calculation of 2 columns (Time difference) How do I export that?

My Blade view
@foreach ($sheet as $sheets)
            <tr>
                <td>{{ $sheets->date }}</td>
                <td>{{ $sheets->Project}}</td>
                <td>{{ $sheets->TicketNumber}}</td>
                <td class="wrapword">{!! clean($sheets->task) !!}</td>
                <td> <?php $date1 = date_create($sheets->time_out);
                             $date2 = date_create($sheets->time_in);
                             $mins = date_diff($date1,$date2);
                             $hour = $mins->format('%h');
                             echo $hour.' Hours <br />'.$mins->format('%i').' Minutes <br />'.$mins->format('%s').' Seconds';?></td>
                 <td class="center"><a class ="delete" href ="{{URL::to('/regtime/delete/'. $sheets->timesheet_id) }}"><span class="glyphicon glyphicon-remove-sign"></span><a href="{{URL::to('/timesheetedit/'.$sheets->timesheet_id) }}"><span class ="glyphicon glyphicon-pencil"></span></a>
                 </td>
            </tr>
        @endforeach

and this is the button in my blade folder responsible for triggering the action

<a href="{{ url('downloadExcelS/csv') }}"><button id="seee" class="btn btn-success btn-lg">Download CSV</button></a>

This is my route

Route::get('downloadExcelS/{type}', 'DemoController@downloadExcelS');

And finally this is my function in the DemoController

public function downloadExcelS(Request $request, $type)
    {
        $data = sheets::select('date', 'Project', 'TicketNumber', 'task')->get();
        return Excel::create('sheets', function($excel) use ($data) {
            $excel->sheet('mySheet', function($sheet) use ($data)
            {
                $sheet->fromArray($data);
            });
        })->download($type);
    }
0 likes
1 reply
matthewmm's avatar
matthewmm
OP
Best Answer
Level 1

Solved it, by using the ->loadView() to export from a table in a view :)

Please or to participate in this conversation.