Skip to content

Commit 9dab9c1

Browse files
committed
Add method to set export filename during runtime.
1 parent 7d13998 commit 9dab9c1

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/Services/DataTable.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ abstract class DataTable implements DataTableContract, DataTableButtonsContract
5151
*/
5252
protected $scopes = [];
5353

54+
/**
55+
* Export filename.
56+
*
57+
* @var string
58+
*/
59+
protected $filename = '';
60+
5461
/**
5562
* @param \Yajra\Datatables\Datatables $datatables
5663
* @param \Illuminate\Contracts\View\Factory $viewFactory
@@ -120,7 +127,7 @@ public function excel()
120127
*/
121128
protected function buildExcelFile()
122129
{
123-
return app('excel')->create($this->filename(), function (LaravelExcelWriter $excel) {
130+
return app('excel')->create($this->getFilename(), function (LaravelExcelWriter $excel) {
124131
$excel->sheet('exported-data', function (LaravelExcelWorksheet $sheet) {
125132
$sheet->fromArray($this->getDataForExport());
126133
});
@@ -290,6 +297,29 @@ public function addScope(DataTableScopeContract $scope)
290297
return $this;
291298
}
292299

300+
/**
301+
* Get export filename.
302+
*
303+
* @return string
304+
*/
305+
public function getFilename()
306+
{
307+
return $this->filename ?: $this->filename();
308+
}
309+
310+
/**
311+
* Set export filename.
312+
*
313+
* @param string $filename
314+
* @return DataTable
315+
*/
316+
public function setFilename($filename)
317+
{
318+
$this->filename = $filename;
319+
320+
return $this;
321+
}
322+
293323
/**
294324
* Apply query scopes.
295325
*

0 commit comments

Comments
 (0)