From 3bd7bde7cd431934d69b1b4a1fd3a66bcb2756f2 Mon Sep 17 00:00:00 2001 From: Supun Dulara Date: Sun, 14 Jul 2024 14:43:15 +0530 Subject: [PATCH] add toggle button to enable or disable --- app/Http/Livewire/Backend/AnnouncementTable.php | 12 +++++++++++- app/Http/Livewire/Backend/EventsTable.php | 14 ++++++++++++-- app/Http/Livewire/Backend/NewsTable.php | 12 +++++++++++- .../views/backend/announcements/enabled_toggle.php | 7 +++++++ .../announcements/index-table-row.blade.php | 4 ++-- resources/views/backend/event/enabled_toggle.php | 7 +++++++ .../views/backend/event/index-table-row.blade.php | 5 +++-- resources/views/backend/news/enabled_toggle.php | 7 +++++++ .../views/backend/news/index-table-row.blade.php | 4 ++-- routes/backend/event.php | 1 + 10 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 resources/views/backend/announcements/enabled_toggle.php create mode 100644 resources/views/backend/event/enabled_toggle.php create mode 100644 resources/views/backend/news/enabled_toggle.php diff --git a/app/Http/Livewire/Backend/AnnouncementTable.php b/app/Http/Livewire/Backend/AnnouncementTable.php index 1467e4d..7a47f40 100644 --- a/app/Http/Livewire/Backend/AnnouncementTable.php +++ b/app/Http/Livewire/Backend/AnnouncementTable.php @@ -26,7 +26,10 @@ public function columns(): array Column::make("Message", "message") ->searchable(), Column::make("Enabled", "enabled") - ->searchable(), + ->sortable() + ->format(function (Announcement $announcement) { + return view('backend.announcement.enabled-toggle', ['announcement' => $announcement]); + }), Column::make("Start", "starts_at") ->sortable(), Column::make("End", "ends_at") @@ -42,6 +45,13 @@ public function query(): Builder ->when($this->getFilter('type'), fn ($query, $type) => $query->where('type', $type)); } + public function toggleEnable($announcementId) + { + $announcement = Announcement::findOrFail($announcementId); + $announcement->enabled = !$announcement->enabled; + $announcement->save(); + } + public function filters(): array { $type = ["" => "Any"]; diff --git a/app/Http/Livewire/Backend/EventsTable.php b/app/Http/Livewire/Backend/EventsTable.php index 08c1fff..06dbccd 100644 --- a/app/Http/Livewire/Backend/EventsTable.php +++ b/app/Http/Livewire/Backend/EventsTable.php @@ -10,7 +10,7 @@ class EventsTable extends DataTableComponent { - public array $perPageAccepted = [25, 50, 100]; + public array $perPageAccepted = [4, 10, 100]; public bool $perPageAll = true; @@ -25,7 +25,10 @@ public function columns(): array ->searchable(), Column::make('Link Caption'), Column::make("Enabled", "enabled") - ->searchable(), + ->sortable() + ->format(function (Event $event) { + return view('backend.event.enabled-toggle', ['event' => $event]); + }), Column::make("Start Time", "start_at") ->searchable(), Column::make("End Time", "end_at") @@ -47,6 +50,13 @@ public function query(): Builder ->when($this->getFilter('type'), fn ($query, $type) => $query->where('type', $type)); } + public function toggleEnable($eventId) + { + $event = Event::findOrFail($eventId); + $event->enabled = !$event->enabled; + $event->save(); + } + // public function filters(): array // { // $type = ["" => "Any"]; diff --git a/app/Http/Livewire/Backend/NewsTable.php b/app/Http/Livewire/Backend/NewsTable.php index ffd2632..b43428d 100644 --- a/app/Http/Livewire/Backend/NewsTable.php +++ b/app/Http/Livewire/Backend/NewsTable.php @@ -25,7 +25,10 @@ public function columns(): array ->searchable(), Column::make('Link Caption'), Column::make("Enabled", "enabled") - ->searchable(), + ->sortable() + ->format(function (News $news) { + return view('backend.news.enabled-toggle', ['news' => $news]); + }), Column::make("Created At", "created_at") ->sortable(), Column::make("Updated At", "updated_at") @@ -41,6 +44,13 @@ public function query(): Builder ->when($this->getFilter('type'), fn ($query, $type) => $query->where('type', $type)); } + public function toggleEnable($newsId) + { + $news = News::findOrFail($newsId); + $news->enabled = !$news->enabled; + $news->save(); + } + // public function filters(): array // { // $type = ["" => "Any"]; diff --git a/resources/views/backend/announcements/enabled_toggle.php b/resources/views/backend/announcements/enabled_toggle.php new file mode 100644 index 0000000..7bda5a8 --- /dev/null +++ b/resources/views/backend/announcements/enabled_toggle.php @@ -0,0 +1,7 @@ +@if($announcement->enabled) + +@else + +@endif diff --git a/resources/views/backend/announcements/index-table-row.blade.php b/resources/views/backend/announcements/index-table-row.blade.php index 9e8f78f..40ae40e 100644 --- a/resources/views/backend/announcements/index-table-row.blade.php +++ b/resources/views/backend/announcements/index-table-row.blade.php @@ -18,9 +18,9 @@ @if($row->enabled) - + @else - + @endif diff --git a/resources/views/backend/event/enabled_toggle.php b/resources/views/backend/event/enabled_toggle.php new file mode 100644 index 0000000..4daf0c8 --- /dev/null +++ b/resources/views/backend/event/enabled_toggle.php @@ -0,0 +1,7 @@ +@if($event->enabled) + +@else + +@endif diff --git a/resources/views/backend/event/index-table-row.blade.php b/resources/views/backend/event/index-table-row.blade.php index 153fccf..d3dbf65 100644 --- a/resources/views/backend/event/index-table-row.blade.php +++ b/resources/views/backend/event/index-table-row.blade.php @@ -30,14 +30,15 @@ @if($row->enabled) - + @else - + @endif +
{{ $row->start_at }} diff --git a/resources/views/backend/news/enabled_toggle.php b/resources/views/backend/news/enabled_toggle.php new file mode 100644 index 0000000..0ba596b --- /dev/null +++ b/resources/views/backend/news/enabled_toggle.php @@ -0,0 +1,7 @@ +@if($news->enabled) + +@else + +@endif diff --git a/resources/views/backend/news/index-table-row.blade.php b/resources/views/backend/news/index-table-row.blade.php index 7f10e14..075b9dc 100644 --- a/resources/views/backend/news/index-table-row.blade.php +++ b/resources/views/backend/news/index-table-row.blade.php @@ -30,9 +30,9 @@ @if($row->enabled) - + @else - + @endif diff --git a/routes/backend/event.php b/routes/backend/event.php index 58e4519..1e8f2d9 100644 --- a/routes/backend/event.php +++ b/routes/backend/event.php @@ -36,6 +36,7 @@ ->push(__('Edit')); }); + // Update Route::put('event/{event}', [EventController::class, 'update']) ->name('event.update');