Skip to content

Commit f5355c6

Browse files
committed
Add feature to append a resource on json response. Fix #277
1 parent 8f7a599 commit f5355c6

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
- [Laravel Excel](https://github.com/Maatwebsite/Laravel-Excel)
2323
- [Laravel Collective HTML & Forms](https://github.com/LaravelCollective/html)
2424
- Automatic registration of Datatables facade.
25-
- HTML Builder with javascripts from template.
25+
- HTML Builder with javascript from template.
2626
- HTML Builder column render now accepts a string, view or closure.
27+
- Add resource on json response by using `->with('key', 'value')` method. #277
2728

2829
###v5.12.5
2930
- Get order column name from the request. Fix #307.

src/Engines/BaseEngine.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ abstract class BaseEngine implements DataTableEngineContract
166166
*/
167167
protected $serializer;
168168

169+
/**
170+
* Array of data to append on json response.
171+
*
172+
* @var array
173+
*/
174+
private $appends;
175+
169176
/**
170177
* Setup search keyword.
171178
*
@@ -675,11 +682,11 @@ abstract public function paging();
675682
*/
676683
public function render($object = false)
677684
{
678-
$output = [
679-
'draw' => (int) $this->request['draw'],
680-
'recordsTotal' => $this->totalRecords,
681-
'recordsFiltered' => $this->filteredRecords,
682-
];
685+
$output = $this->appends + [
686+
'draw' => (int) $this->request['draw'],
687+
'recordsTotal' => $this->totalRecords,
688+
'recordsFiltered' => $this->filteredRecords,
689+
];
683690

684691
if (isset($this->transformer)) {
685692
$fractal = new Manager();
@@ -800,4 +807,24 @@ public function isCaseInsensitive()
800807
{
801808
return Config::get('datatables.search.case_insensitive', false);
802809
}
810+
811+
/**
812+
* Append data on json response.
813+
*
814+
* @param mixed $key
815+
* @param mixed $value
816+
* @return $this
817+
*/
818+
public function with($key, $value = '')
819+
{
820+
if (is_array($key)) {
821+
$this->appends = $key;
822+
} elseif (is_callable($value)) {
823+
$this->appends[$key] = value($value);
824+
} else {
825+
$this->appends[$key] = value($value);
826+
}
827+
828+
return $this;
829+
}
803830
}

0 commit comments

Comments
 (0)