Add Profile Comment Search (Livewire)

This commit is contained in:
2026-01-10 18:55:47 +01:00
parent 7810cd53fb
commit cfd6af59fb
5 changed files with 178 additions and 60 deletions

View File

@@ -3,8 +3,6 @@
namespace App\Http\Controllers;
use App\Models\Episode;
use App\Models\Playlist;
use App\Models\PlaylistEpisode;
use App\Models\User;
use App\Http\Requests\ProfileUpdateRequest;
use Illuminate\Http\RedirectResponse;
@@ -84,7 +82,7 @@ class ProfileController extends Controller
public function comments(Request $request): View
{
return view('profile.comments', [
'user' => $request->user(),
'user' => $request->user(),
]);
}

View File

@@ -0,0 +1,49 @@
<?php
namespace App\Livewire;
use App\Models\Comment;
use Livewire\Component;
use Livewire\WithPagination;
class UserComments extends Component
{
use WithPagination;
public $model;
public $commentSearch;
public $order = 'created_at_desc';
public function render()
{
$orderby = 'created_at';
$orderdirection = 'desc';
switch ($this->order) {
case 'created_at_desc':
$orderby = 'created_at';
$orderdirection = 'desc';
break;
case 'created_at_asc':
$orderby = 'created_at';
$orderdirection = 'asc';
break;
default:
$orderby = 'created_at';
$orderdirection = 'desc';
}
$comments = Comment::where('user_id', $this->model->id)
->when($this->commentSearch != '', fn ($query) => $query->where('body', 'like', '%'.$this->commentSearch.'%'))
->orderBy($orderby, $orderdirection)
->paginate(10);
return view('livewire.user-comments', [
'comments' => $comments
]);
}
}

View File

@@ -89,9 +89,17 @@ class User extends Authenticatable
/**
* Has Many Comments.
*/
public function comments()
{
return $this->hasMany(Comment::class, 'user_id');
}
/**
* Get Comment Count.
*/
public function commentCount(): int
{
return DB::table('comments')->where('commenter_id', $this->id)->count();
return cache()->remember('userComments' . $this->id, 300, fn() => $this->comments->count());
}
/**