Add Profile Comment Search (Livewire)
This commit is contained in:
@@ -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(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
49
app/Livewire/UserComments.php
Normal file
49
app/Livewire/UserComments.php
Normal 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
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user