Hi @laura123
It exports everything because you're not passing the month to TimesheetExport class, instead you're getting all records with Timesheet::all().
You could pass the month to the export by using a constructor and the using something along the lines Timesheet::where('month', $this->month)->get()
Or yet, a much simpler way would be to send the already filtered data that you display on screen to the TimesheetExport via the constructor (this way you'd save a DB call).
send filtered data to excel? keeps sending all data from table :(
I have filtered my view so when I select a month from the drop down box I only see entries for that month, but when i try to export the file, it will include everything,
I have no idea how to only send the filtered data through, I've looked at the Laravel documentation and i can see how to send a view (which I'm doing) but cant work out to send the correct data
The Blade View
<body>
<div class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div class="col">
<h1>Hello {{ Auth::user()->Name }}
, welcome to your Timesheet for {{ \Carbon\Carbon::now()->format('F') }}
</h1>
<div class="col-sm-15 sidenav hidden-xs">
<div class="col-sm-15 sidenav hidden-xs">
<div class="container text-center">
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#exampleModal">
Submit
Timesheet
</button>
</div>
</div>
<!-- Modal pop up box -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<h3>"Are you happy to send timesheet through?</h3>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<a class="btn btn-success" href="{{ route('timesheet.export_view') }}">Submit
</a>
</div>
</div>
</div>
</div>
</div>
{{-- Select month drop down --}}
<form class="form-horizontal" method="POST" action="{{ route('timesheet.filter') }}">
@csrf
<div class="form-group">
<label for="">Select Month:</label>
<select name="month" class="form_control">
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<input type="submit" value="Submit">
</form>
</div>
<form class="form-horizontal" method="POST" action="{{ route('timesheet.store') }}">
@csrf
<table class="table table-sm table-success">
<thead>
<tr>
<th scope="col">DAY/DATE</th>
<th scope="col">SHIFT</th>
<th scope="col">STATION</th>
<div class=" table-sm table-danger">
<th scope="col">Overtime</th>
<th scope="col">Weekday Night</th>
<th scope="col">Sat Unsocial</th>
{{-- <th scope="col">Sat High</th> --}}
<th scope="col">Sun Unsocial</th>
<th scope="col">Bank Holiday</th>
<th scope="col">Notes</th>
</div>
</tr>
</thead>
@foreach ($shifts as $shift)
<tbody class="table-light">
<tr>
<div class="form-group">
<td>
{{ \Carbon\Carbon::parse($shift->pivot->Date)->format('D d-M') }}
</td>
<td>{{ $shift->Name }} </td>
<td>{{ $station->Name }}</td>
<td>{{ 'not done this' }}</td>
<td>
@if (Carbon\Carbon::parse($shift->pivot->Date)->isWeekday() and Carbon\Carbon::parse($shift->pivot->Date) != $newYear and Carbon\Carbon::parse($shift->pivot->Date) != $augBH and Carbon\Carbon::parse($shift->pivot->Date) != $bDay and Carbon\Carbon::parse($shift->Start_Time) > Carbon\Carbon::parse('17:00'))
{{ $shift->Hours }}
@endif
</td>
<td>
@if (Carbon\Carbon::parse($shift->pivot->Date)->dayOfWeek == Carbon\Carbon::SATURDAY and Carbon\Carbon::parse($shift->Start_Time) < Carbon\Carbon::parse('18:00'))
{{ $shift->SAT_Low_Unsocial }}
@elseif (Carbon\Carbon::parse($shift->pivot->Date)->dayOfWeek ==
Carbon\Carbon::SATURDAY
and
Carbon\Carbon::parse($shift->Start_Time) > Carbon\Carbon::parse('18:00'))
{{ $shift->SAT_Low_Unsocial }}
@endif
</td>
<td>
@if (Carbon\Carbon::parse($shift->pivot->Date)->dayOfWeek == Carbon\Carbon::SUNDAY and Carbon\Carbon::parse($shift->Start_Time) < Carbon\Carbon::parse('18:00'))
{{ $shift->SUN_High_Unsocial }}
@elseif (Carbon\Carbon::parse($shift->pivot->Date)->dayOfWeek ==
Carbon\Carbon::SUNDAY
and
Carbon\Carbon::parse($shift->Start_Time) > Carbon\Carbon::parse('18:00'))
{{ $shift->SUN_High_Unsocial }}
@endif
</td>
<td>
@if (Carbon\Carbon::parse($shift->pivot->Date) == $bDay or Carbon\Carbon::parse($shift->pivot->Date) == $newYear or Carbon\Carbon::parse($shift->pivot->Date) == $augBH)
{{ $shift->Hours }}
@endif
</td>
</tr>
</tbody>
@endforeach
</table>
</div>
</body>
</form>
The controller that filters the view
public function filter(Request $request) { $user = Auth::user();
$month = $request->month;
$shifts = $user->shifts()->whereMonth('Date', $month)->get();
$station = $user->Station;
$endDate = Carbon::today()->addDays(7);
$date = Carbon::today();
$xmas = new Carbon('2021-12-27');
$bDay = new Carbon('2021-12-28');
$newYear = new Carbon('2022-01-03');
$augBH = new Carbon('2021-08-30');
$grade = $user->Grade;
return view('layouts/timesheet/show', ['user' => $user, 'station' => $station, 'grade' => $grade, 'xmas' => $xmas, 'bDay' => $bDay, 'newYear' => $newYear, 'augBH' => $augBH, 'date' => $date, 'endDate' => $endDate, 'shifts' => $shifts]);
}
The Export_View
class TimesheetExportView implements FromView { /** * @return \Illuminate\Support\Collection */ public function view(): View {
$date = Carbon::today();
$user = Auth::user();
$station = $user->Station;
$grade = $user->Grade;
$endDate = Carbon::today()->addDays(7);
$xmas = new Carbon('2021-12-27');
$bDay = new Carbon('2021-12-28');
$newYear = new Carbon('2022-01-03');
$augBH = new Carbon('2021-08-30');
return view('layouts/timesheet/table', [
'user' => $user, 'station' => $station, 'grade' => $grade, 'xmas' => $xmas, 'bDay' => $bDay, 'newYear' => $newYear, 'augBH' => $augBH, 'date' => $date, 'endDate' => $endDate
]);
}
}
The Export ( i know this shouldn't be all, I keep trying things and breaking it)
class TimesheetExport implements FromCollection { /** * @return \Illuminate\Support\Collection */ public function collection() { return Timesheet::all(); } }
Id be so grateful for any help, or even if there are any websites/videos i can follow that may help? i cant seem to find much out there on this Thank you
Hi @laura123
You could pass the month in the following way.
Create a new route for exporting which will accept the month number as the argument, something along the lines '/shift/{month}/export', [TimesheetController::class, 'export'] and create a form that will create a post request to the new route
Then create a new method in the TimesheetController
public function export ($month) {
return Excel::download(new TimesheetExport($month), 'shifts.xlsx');
}
and finally, in the TimesheetExport you could have the following code:
class TimesheetExport implements FromCollection
{
protected $month= [];
public function __construct($month)
{
return $this->month= $month;
}
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
$data = Timesheet::where('month', $this->month)->get();
return $data;
}
You can finetune the query as needed.
Please or to participate in this conversation.