Skip to content

Commit f8fe73c

Browse files
Update example table (#149)
1 parent a16171a commit f8fe73c

File tree

3 files changed

+56
-35
lines changed

3 files changed

+56
-35
lines changed

docs/getting-started/example.md

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use App\Models\User;
1515
use Illuminate\Database\Eloquent\Collection;
1616
use Illuminate\Database\Eloquent\Model;
1717
use RamonRietdijk\LivewireTables\Actions\Action;
18+
use RamonRietdijk\LivewireTables\Columns\ActionColumn;
1819
use RamonRietdijk\LivewireTables\Columns\BooleanColumn;
1920
use RamonRietdijk\LivewireTables\Columns\Column;
2021
use RamonRietdijk\LivewireTables\Columns\DateColumn;
@@ -60,11 +61,7 @@ class BlogTable extends LivewireTable
6061
->sortable()
6162
->format('F jS, Y'),
6263

63-
Column::make(__('Actions'), function (Model $model): string {
64-
return '<a class="underline" href="#'.$model->getKey().'">Edit</a>';
65-
})
66-
->clickable(false)
67-
->asHtml(),
64+
ActionColumn::make(__('Actions')),
6865
];
6966
}
7067

@@ -75,12 +72,12 @@ class BlogTable extends LivewireTable
7572

7673
SelectFilter::make(__('Category'), 'category_id')
7774
->options(
78-
Category::query()->get()->pluck('title', 'id')->toArray()
75+
Category::query()->pluck('title', 'id')->toArray()
7976
),
8077

8178
SelectFilter::make(__('Author'), 'author_id')
8279
->options(
83-
User::query()->get()->pluck('name', 'id')->toArray()
80+
User::query()->pluck('name', 'id')->toArray()
8481
),
8582

8683
DateFilter::make(__('Created At'), 'created_at'),
@@ -95,18 +92,34 @@ class BlogTable extends LivewireTable
9592
})->standalone(),
9693

9794
Action::make(__('Publish'), function (Collection $models): void {
98-
$models->each(function (Blog $blog): void {
99-
$blog->published = true;
100-
$blog->save();
101-
});
102-
}),
95+
foreach ($models as $model) {
96+
$model->published = true;
97+
$model->save();
98+
}
99+
})->canRun(fn (Blog $blog): bool => ! $blog->published),
103100

104101
Action::make(__('Unpublish'), function (Collection $models): void {
105-
$models->each(function (Blog $blog): void {
106-
$blog->published = false;
107-
$blog->save();
108-
});
109-
}),
102+
foreach ($models as $model) {
103+
$model->published = false;
104+
$model->save();
105+
}
106+
})->canRun(fn (Blog $blog): bool => $blog->published),
107+
108+
Action::make(__('Delete'), function (Collection $models): void {
109+
foreach ($models as $model) {
110+
$model->delete();
111+
}
112+
})
113+
->canRun(fn (Blog $blog): bool => ! $blog->trashed())
114+
->record(),
115+
116+
Action::make(__('Restore'), function (Collection $models): void {
117+
foreach ($models as $model) {
118+
$model->restore();
119+
}
120+
})
121+
->canRun(fn (Blog $blog): bool => $blog->trashed())
122+
->record(),
110123
];
111124
}
112125
}

tests/Fakes/Livewire/BlogLivewireTable.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Database\Eloquent\Collection;
88
use RamonRietdijk\LivewireTables\Actions\Action;
9+
use RamonRietdijk\LivewireTables\Columns\ActionColumn;
910
use RamonRietdijk\LivewireTables\Columns\BooleanColumn;
1011
use RamonRietdijk\LivewireTables\Columns\Column;
1112
use RamonRietdijk\LivewireTables\Columns\DateColumn;
@@ -53,6 +54,8 @@ protected function columns(): array
5354
DateColumn::make(__('Created At'), 'created_at')
5455
->sortable()
5556
->format('F jS, Y'),
57+
58+
ActionColumn::make(__('Actions'), null, 'actions'),
5659
];
5760
}
5861

@@ -63,12 +66,12 @@ protected function filters(): array
6366

6467
SelectFilter::make(__('Category'), 'category_id')
6568
->options(
66-
Category::query()->get()->pluck('title', 'id')->toArray()
69+
Category::query()->pluck('title', 'id')->toArray()
6770
),
6871

6972
SelectFilter::make(__('Author'), 'author_id')
7073
->options(
71-
User::query()->get()->pluck('name', 'id')->toArray()
74+
User::query()->pluck('name', 'id')->toArray()
7275
),
7376

7477
DateFilter::make(__('Created At'), 'created_at'),
@@ -83,34 +86,38 @@ protected function actions(): array
8386
})->standalone(),
8487

8588
Action::make(__('Publish'), function (Collection $models): void {
86-
/** @var Blog $model */
89+
/** @var Collection<int, Blog> $models */
8790
foreach ($models as $model) {
8891
$model->published = true;
8992
$model->save();
9093
}
91-
}),
94+
})->canRun(fn (Blog $blog): bool => ! $blog->published),
9295

9396
Action::make(__('Unpublish'), function (Collection $models): void {
94-
/** @var Blog $model */
97+
/** @var Collection<int, Blog> $models */
9598
foreach ($models as $model) {
9699
$model->published = false;
97100
$model->save();
98101
}
99-
}),
102+
})->canRun(fn (Blog $blog): bool => $blog->published),
100103

101104
Action::make(__('Delete'), function (Collection $models): void {
102-
/** @var Blog $model */
105+
/** @var Collection<int, Blog> $models */
103106
foreach ($models as $model) {
104107
$model->delete();
105108
}
106-
})->record(),
109+
})
110+
->canRun(fn (Blog $blog): bool => ! $blog->trashed())
111+
->record(),
107112

108113
Action::make(__('Restore'), function (Collection $models): void {
109-
/** @var Blog $model */
114+
/** @var Collection<int, Blog> $models */
110115
foreach ($models as $model) {
111116
$model->restore();
112117
}
113-
})->record(),
118+
})
119+
->canRun(fn (Blog $blog): bool => $blog->trashed())
120+
->record(),
114121
];
115122
}
116123
}

tests/Integration/Concerns/HasColumnsTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function it_can_select_all_columns(): void
2525
'author_company_name',
2626
'published',
2727
'created_at',
28+
'actions',
2829
])
2930
->call('selectAllColumns', false)
3031
->assertSet('columns', []);
@@ -48,49 +49,49 @@ public static function cases(): array
4849
'from' => 'title',
4950
'to' => 'author_name',
5051
'above' => false,
51-
'order' => ['thumbnail', 'category_title', 'author_name', 'title', 'author_company_name', 'published', 'created_at'],
52+
'order' => ['thumbnail', 'category_title', 'author_name', 'title', 'author_company_name', 'published', 'created_at', 'actions'],
5253
],
5354
'From author_name to title, below the column' => [
5455
'from' => 'author_name',
5556
'to' => 'title',
5657
'above' => false,
57-
'order' => ['thumbnail', 'title', 'author_name', 'category_title', 'author_company_name', 'published', 'created_at'],
58+
'order' => ['thumbnail', 'title', 'author_name', 'category_title', 'author_company_name', 'published', 'created_at', 'actions'],
5859
],
5960
'From title to author_name, above the column' => [
6061
'from' => 'title',
6162
'to' => 'author_name',
6263
'above' => true,
63-
'order' => ['thumbnail', 'category_title', 'title', 'author_name', 'author_company_name', 'published', 'created_at'],
64+
'order' => ['thumbnail', 'category_title', 'title', 'author_name', 'author_company_name', 'published', 'created_at', 'actions'],
6465
],
6566
'From author_name to title, above the column' => [
6667
'from' => 'author_name',
6768
'to' => 'title',
6869
'above' => true,
69-
'order' => ['thumbnail', 'author_name', 'title', 'category_title', 'author_company_name', 'published', 'created_at'],
70+
'order' => ['thumbnail', 'author_name', 'title', 'category_title', 'author_company_name', 'published', 'created_at', 'actions'],
7071
],
7172
'From thumbnail to author_company_name, below the column' => [
7273
'from' => 'thumbnail',
7374
'to' => 'author_company_name',
7475
'above' => false,
75-
'order' => ['title', 'category_title', 'author_name', 'author_company_name', 'thumbnail', 'published', 'created_at'],
76+
'order' => ['title', 'category_title', 'author_name', 'author_company_name', 'thumbnail', 'published', 'created_at', 'actions'],
7677
],
7778
'From author_company_name to thumbnail, above the column' => [
7879
'from' => 'author_company_name',
7980
'to' => 'thumbnail',
8081
'above' => true,
81-
'order' => ['author_company_name', 'thumbnail', 'title', 'category_title', 'author_name', 'published', 'created_at'],
82+
'order' => ['author_company_name', 'thumbnail', 'title', 'category_title', 'author_name', 'published', 'created_at', 'actions'],
8283
],
8384
'From thumbnail to thumbnail, above the column, does not change the order' => [
8485
'from' => 'thumbnail',
8586
'to' => 'thumbnail',
8687
'above' => true,
87-
'order' => ['thumbnail', 'title', 'category_title', 'author_name', 'author_company_name', 'published', 'created_at'],
88+
'order' => ['thumbnail', 'title', 'category_title', 'author_name', 'author_company_name', 'published', 'created_at', 'actions'],
8889
],
8990
'From thumbnail to title, above the column, does not change the order' => [
9091
'from' => 'thumbnail',
9192
'to' => 'title',
9293
'above' => true,
93-
'order' => ['thumbnail', 'title', 'category_title', 'author_name', 'author_company_name', 'published', 'created_at'],
94+
'order' => ['thumbnail', 'title', 'category_title', 'author_name', 'author_company_name', 'published', 'created_at', 'actions'],
9495
],
9596
];
9697
}

0 commit comments

Comments
 (0)