Forked from rappasoft/laravel-livewire-tables-demo-table.php
Created
July 13, 2021 04:12
-
-
Save zhiephie/248f638a6b17dfb6c32f51c77d7d1ba8 to your computer and use it in GitHub Desktop.
Laravel Livewire Tables Demo Table Source
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\Http\Livewire; | |
use App\Models\User; | |
use Rappasoft\LaravelLivewireTables\DataTableComponent; | |
use Rappasoft\LaravelLivewireTables\Views\Column; | |
use Rappasoft\LaravelLivewireTables\Views\Filter; | |
class UsersTable extends DataTableComponent | |
{ | |
public bool $columnSelect = true; | |
public string $defaultSortColumn = 'sort'; | |
public bool $reorderEnabled = true; | |
public bool $hideBulkActionsOnEmpty = true; | |
public array $bulkActions = [ | |
'activate' => 'Activate', | |
'deactivate' => 'Deactivate', | |
]; | |
public function columns(): array | |
{ | |
return [ | |
Column::make('Sort') | |
->sortable(), | |
Column::make('Name') | |
->sortable() | |
->searchable(), | |
Column::make('E-mail', 'email') | |
->sortable() | |
->searchable(), | |
Column::make('Active') | |
->sortable() | |
->format(function ($value) { | |
return view('tables.cells.boolean', | |
[ | |
'boolean' => $value | |
] | |
); | |
}), | |
Column::make('Verified', 'email_verified_at') | |
->sortable() | |
->excludeFromSelectable(), | |
]; | |
} | |
public function filters(): array | |
{ | |
return [ | |
'verified' => Filter::make('E-mail Verified') | |
->select([ | |
'' => 'Any', | |
'yes' => 'Yes', | |
'no' => 'No', | |
]), | |
'active' => Filter::make('Active') | |
->select([ | |
'' => 'Any', | |
'yes' => 'Yes', | |
'no' => 'No', | |
]), | |
'verified_from' => Filter::make('Verified From') | |
->date(), | |
'verified_to' => Filter::make('Verified To') | |
->date(), | |
]; | |
} | |
public function query() | |
{ | |
return User::query() | |
->when($this->getFilter('verified'), function ($query, $verified) { | |
if ($verified === 'yes') { | |
return $query->whereNotNull('verified'); | |
} | |
return $query->whereNull('verified'); | |
}) | |
->when($this->getFilter('active'), fn($query, $active) => $query->where('active', $active === 'yes')) | |
->when($this->getFilter('verified_from'), fn($query, $date) => $query->where('email_verified_at', '>=', $date)) | |
->when($this->getFilter('verified_to'), fn($query, $date) => $query->where('email_verified_at', '<=', $date)); | |
} | |
public function reorder($items): void | |
{ | |
foreach ($items as $item) { | |
optional(User::find((int)$item['value']))->update(['sort' => (int)$item['order']]); | |
} | |
} | |
public function activate(): void | |
{ | |
if ($this->selectedRowsQuery->count() > 0) { | |
User::whereIn('id', $this->selectedKeys())->update(['active' => true]); | |
} | |
$this->selected = []; | |
$this->resetBulk(); | |
} | |
public function deactivate(): void | |
{ | |
if ($this->selectedRowsQuery->count() > 0) { | |
User::whereIn('id', $this->selectedKeys())->update(['active' => false]); | |
} | |
$this->resetBulk(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment