Created
December 23, 2017 03:10
-
-
Save chriscalip/8b71118c72518f19806d73a135fdb507 to your computer and use it in GitHub Desktop.
Explaining issue https://github.com/yajra/laravel-datatables-editor/issues/1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\DataTables; | |
use App\Asset; | |
use Yajra\DataTables\Services\DataTable; | |
class AssetsDataTable extends DataTable | |
{ | |
/** | |
* Build DataTable class. | |
* | |
* @param mixed $query Results from query() method. | |
* @return \Yajra\DataTables\DataTableAbstract | |
*/ | |
public function dataTable($query) | |
{ | |
return datatables($query)->setRowId('id'); | |
} | |
/** | |
* Get query source of dataTable. | |
* | |
* @param \App\Asset $model | |
* @return \Illuminate\Database\Eloquent\Builder | |
*/ | |
public function query(Asset $model) | |
{ | |
return $model->newQuery()->select('id', 'name'); | |
} | |
/** | |
* Optional method if you want to use html builder. | |
* | |
* @return \Yajra\DataTables\Html\Builder | |
*/ | |
public function html() | |
{ | |
return $this->builder() | |
->columns($this->getColumns()) | |
->minifiedAjax() | |
->parameters([ | |
'dom' => 'Bfrtip', | |
'order' => [1, 'asc'], | |
'select' => [ | |
'style' => 'os', | |
'selector' => 'td:first-child', | |
], | |
'buttons' => [ | |
['extend' => 'create', 'editor' => 'editor'], | |
['extend' => 'edit', 'editor' => 'editor'], | |
['extend' => 'remove', 'editor' => 'editor'], | |
['extend' => 'publish', 'editor' => 'editor'], | |
] | |
]); | |
} | |
/** | |
* Get columns. | |
* | |
* @return array | |
*/ | |
protected function getColumns() | |
{ | |
return [ | |
[ | |
'data' => null, | |
'defaultContent' => '', | |
'className' => 'select-checkbox', | |
'title' => '', | |
'orderable' => false, | |
'searchable' => false | |
], | |
'id', | |
'name', | |
]; | |
} | |
/** | |
* Get filename for export. | |
* | |
* @return string | |
*/ | |
protected function filename() | |
{ | |
return 'assets_' . time(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\DataTables; | |
use App\Asset; | |
use Illuminate\Validation\Rule; | |
use Yajra\DataTables\DataTablesEditor; | |
use Illuminate\Database\Eloquent\Model; | |
class AssetsDataTablesEditor extends DataTablesEditor | |
{ | |
protected $model = Asset::class; | |
protected $actions = ['create', 'edit', 'remove', 'publish']; | |
/** | |
* Get create action validation rules. | |
* | |
* @return array | |
*/ | |
public function createRules() | |
{ | |
return [ | |
'name' => 'required', | |
]; | |
} | |
/** | |
* Get edit action validation rules. | |
* | |
* @param Model $model | |
* @return array | |
*/ | |
public function editRules(Model $model) | |
{ | |
return [ | |
'name' => 'sometimes|required', | |
]; | |
} | |
/** | |
* Get remove action validation rules. | |
* | |
* @param Model $model | |
* @return array | |
*/ | |
public function removeRules(Model $model) | |
{ | |
return []; | |
} | |
public function creating(Model $model, array $data) | |
{ | |
return $data; | |
} | |
public function updating(Model $model, array $data) | |
{ | |
return $data; | |
} | |
/** | |
* Process publish action request. | |
* | |
* @param Request $request | |
* @return JsonResponse | |
*/ | |
public function publish(Request $request) | |
{ | |
$instance = $this->resolveModel(); | |
$connection = $instance->getConnection(); | |
$affected = []; | |
$errors = []; | |
$connection->beginTransaction(); | |
foreach ($request->get('data') as $key => $data) { | |
$model = $instance->newQuery()->find($key); | |
$validator = $this->getValidationFactory()->make($data, $this->editRules($model)); | |
if ($validator->fails()) { | |
foreach ($this->formatErrors($validator) as $error) { | |
$errors[] = $error; | |
}; | |
continue; | |
} | |
if (method_exists($this, 'publishing')) { | |
$data = $this->publishing($model, $data); | |
} | |
$model->update($data); | |
if (method_exists($this, 'published')) { | |
$this->updated($model, $data); | |
} | |
$model->setAttribute('DT_RowId', $model->getKey()); | |
$affected[] = $model; | |
} | |
if (! $errors) { | |
$connection->commit(); | |
} else { | |
$connection->rollBack(); | |
} | |
return $this->toJson($affected, $errors); | |
} | |
public function publishing(Model $model, array $data) | |
{ | |
$data['status'] = 1; | |
return $data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment