public function filterCardsByUser($userId)
{
$this->cards = ProjectCard::with([
'users', 'files', 'comments', 'comments.user',
'comments.tag', 'tasks', 'tag'
])
->where('kanban_id', $this->kanban->id)
->whereHas('users', function ($query) use ($userId) {
$query->where('user', $userId);
})
->orderByDesc('updated_at')
->get();
}
Docs: https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence
Or with relationship
public function filterCardsByUser($userId)
{
$this->cards = $this->kanban->projectCards()->with([
'users', 'files', 'comments', 'comments.user',
'comments.tag', 'tasks', 'tag'
])
->whereHas('users', function ($query) use ($userId) {
$query->where('user', $userId);
})
->orderByDesc('updated_at')
->get();
}