Admin: Add ability to delete all coments from user

This commit is contained in:
2025-10-28 16:20:40 +01:00
parent efb3e4197b
commit 59d63abd79
2 changed files with 19 additions and 2 deletions

View File

@@ -8,6 +8,8 @@ use Livewire\Component;
use Livewire\WithPagination; use Livewire\WithPagination;
use Livewire\Attributes\Url; use Livewire\Attributes\Url;
use Illuminate\Support\Facades\DB;
class AdminUserSearch extends Component class AdminUserSearch extends Component
{ {
use WithPagination; use WithPagination;
@@ -24,6 +26,18 @@ class AdminUserSearch extends Component
#[Url(history: true)] #[Url(history: true)]
public $banned = []; public $banned = [];
public function deleteUserComments(int $userID)
{
$user = User::where('id', $userID)
->firstOrFail();
DB::table('comments')
->where('commenter_id', '=', $user->id)
->delete();
cache()->flush();
}
public function render() public function render()
{ {
$users = User::when($this->filtered !== [], fn ($query) => $query->where('id', '>=', 10000)) $users = User::when($this->filtered !== [], fn ($query) => $query->where('id', '>=', 10000))

View File

@@ -74,15 +74,18 @@
<td class="px-6 py-4"> <td class="px-6 py-4">
{{ $user->updated_at->format('Y-m-d') }} {{ $user->updated_at->format('Y-m-d') }}
</td> </td>
<td class="px-6 py-4"> <td class="px-6 py-4 flex flex-col gap-1">
<form method="POST" action="{{ route('admin.user.update') }}"> <form method="POST" action="{{ route('admin.user.update') }}">
@csrf @csrf
<input type="hidden" value="{{ $user->id }}" name="id"> <input type="hidden" value="{{ $user->id }}" name="id">
<input type="hidden" value="{{ $user->is_banned ? 'unban' : 'ban' }}" name="action"> <input type="hidden" value="{{ $user->is_banned ? 'unban' : 'ban' }}" name="action">
<button type="submit" class="inline-block rounded bg-rose-600 pl-[4px] pr-[4px] p-[1px] text-xs font-medium uppercase leading-normal text-white transition duration-150 ease-in-out hover:bg-rose-700 focus:bg-rose-600"> <button type="submit" class="inline-block w-full rounded bg-rose-600 pl-[4px] pr-[4px] p-[1px] text-xs font-medium uppercase leading-normal text-white transition duration-150 ease-in-out hover:bg-rose-700 focus:bg-rose-600">
{{ $user->is_banned ? 'Unban' : 'Ban' }} {{ $user->is_banned ? 'Unban' : 'Ban' }}
</button> </button>
</form> </form>
<button wire:click="deleteUserComments('{{ $user->id }}')" class="inline-block w-full rounded bg-red-600 pl-[4px] pr-[4px] p-[1px] text-xs font-medium uppercase leading-normal text-white transition duration-150 ease-in-out hover:bg-rose-700 focus:bg-rose-600">
Delete comments
</button>
</td> </td>
</tr> </tr>
@endforeach @endforeach