Assuming that the $guardians variable is an array of valid ID's.
if ($request->filled('guardians')) {
$guardians = $request->guardians;
$query->whereHas('guardians', function ($q) use ($guardians) {
$q->whereIn('id', $guardians); // Note that I've changed `guardian_id` to `id` and that depends on your database structure so if this doesn't work try changing it back to `guardian_id`
});
}