199 lines
3.8 KiB
PHP
199 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
use Livewire\Component;
|
|
use Livewire\WithPagination;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Datatable extends Component
|
|
{
|
|
use WithPagination, Traits\WithSort, Traits\Alert;
|
|
|
|
/**
|
|
* Query string settings.
|
|
*/
|
|
protected $queryString = [
|
|
'sort' => ['except' => 'id'],
|
|
'dir' => ['except' => 'asc'],
|
|
'page' => ['except' => 1],
|
|
'trashed' => ['except' => false]
|
|
];
|
|
|
|
/**
|
|
* Model to get data from.
|
|
*/
|
|
public $model;
|
|
|
|
/**
|
|
* Array of columns to show
|
|
*/
|
|
public array $columns;
|
|
|
|
/**
|
|
* What colums are sortable
|
|
*/
|
|
public array $sort_columns;
|
|
|
|
/**
|
|
* Default sorting column
|
|
*/
|
|
public string $default_sort;
|
|
|
|
/**
|
|
* If only trashed records should be shown.
|
|
*/
|
|
public bool $trashed = false;
|
|
|
|
public $route_create;
|
|
|
|
/**
|
|
* Route for editing a record (if null, link is omitted)
|
|
*/
|
|
public $route_edit;
|
|
|
|
/**
|
|
* True if delete functionality is enabled.
|
|
*/
|
|
public $delete_enabled;
|
|
|
|
/**
|
|
* True if restore functionality is enabled.
|
|
*/
|
|
public $restore_enabled;
|
|
|
|
/**
|
|
* How many records should be displayed on one page.
|
|
*/
|
|
public int $itemsPerPage = 30;
|
|
|
|
public function mount(string $model, array $columns,
|
|
array $sort_columns = [], $default_sort = '',
|
|
$route_create = null, $route_edit = null, $delete_enabled = false, $restore_enabled = false)
|
|
{
|
|
$this->model = app()->make($model);
|
|
$this->default_sort = $default_sort;
|
|
$this->sort = $this->default_sort;
|
|
$this->sort_columns = $sort_columns;
|
|
$this->route_create = $route_create;
|
|
$this->route_edit = $route_edit;
|
|
$this->delete_enabled = (bool) $delete_enabled;
|
|
$this->restore_enabled = (bool) $restore_enabled;
|
|
|
|
$this->setTrashed(false);
|
|
}
|
|
|
|
/**
|
|
* Get the model name.
|
|
*/
|
|
public function getModelName() : string
|
|
{
|
|
return class_basename($this->model);
|
|
}
|
|
|
|
/**
|
|
* Delete an record
|
|
*/
|
|
public function delete($id)
|
|
{
|
|
if (!$this->delete_enabled) {
|
|
return;
|
|
}
|
|
|
|
$record = $this->model->find($id);
|
|
|
|
if (!$record) {
|
|
$this->danger('Record not found');
|
|
return;
|
|
}
|
|
|
|
$record->delete();
|
|
|
|
$this->info(__($this->getModelName() . " #:id was deleted.", [ 'id' => $record->id ]));
|
|
}
|
|
|
|
/**
|
|
* Restore a delted record
|
|
*/
|
|
public function restore($id)
|
|
{
|
|
if (!$this->restore_enabled) {
|
|
return;
|
|
}
|
|
|
|
$record = $this->model->withTrashed()->find($id);
|
|
|
|
if (!$record) {
|
|
$this->danger('Record not found');
|
|
return;
|
|
}
|
|
|
|
$record->restore();
|
|
|
|
$this->info(__($this->getModelName() . " #:id was restored.", [ 'id' => $record->id ]));
|
|
}
|
|
|
|
/**
|
|
* Render the datatable
|
|
*/
|
|
public function render()
|
|
{
|
|
$query = $this->model->query();
|
|
|
|
// Only include trashed if asked to.
|
|
if ($this->trashed) {
|
|
$query->onlyTrashed();
|
|
}
|
|
|
|
// Check colums for "dot-notated" strings.
|
|
// as we want to eager load those relationships
|
|
foreach ($this->columns as $col => $_) {
|
|
$p = strrpos($col, '.');
|
|
if ($p !== false) {
|
|
$rel = substr($col, 0, $p);
|
|
$query->with($rel);
|
|
}
|
|
}
|
|
|
|
// Apply sorting.
|
|
$this->sortApply($query);
|
|
|
|
$items = $query->paginate($this->itemsPerPage);
|
|
|
|
return view('livewire.datatable', ['items' => $items]);
|
|
}
|
|
|
|
/**
|
|
* Toggle trashed flag
|
|
*/
|
|
public function toggleTrashed()
|
|
{
|
|
$this->setTrashed(!$this->trashed);
|
|
}
|
|
|
|
/**
|
|
* Set trashed flag.
|
|
*/
|
|
public function setTrashed($value)
|
|
{
|
|
$this->trashed = (bool) $value;
|
|
|
|
if ($this->trashed) {
|
|
// Add deleted_at columns.
|
|
$this->columns['deleted_at'] = 'Deleted at';
|
|
$this->sort_columns['deleted_at'] = 'deleted_at';
|
|
} else {
|
|
// Unset deleted_at columns.
|
|
unset($this->columns['deleted_at']);
|
|
unset($this->sort_columns['deleted_at']);
|
|
|
|
// if delete_at was the sorting column
|
|
// revert back to default sorting.
|
|
if ($this->sort == 'deleted_at') {
|
|
$this->sort = $this->default_sort;
|
|
}
|
|
}
|
|
}
|
|
}
|