The issue seems to be with the data format being returned from the backend. In the first example, you are returning a query builder instance, which can be further manipulated on the frontend. However, in the second example, you are returning a collection of patients with only the 'name' and 'email' fields, which cannot be further manipulated on the frontend.
To fix this, you can return the full patient data from the backend and use a frontend data filter to filter the data. Here's an example of how you can modify your controller code:
'patients' => Patient::query()
->when($search, fn ($query) => $query->where( ... ))
->with(['nok', 'insurance'])
->orderByDesc('created_at')
->get(),
Then, in your frontend code, you can use a data filter to filter the data based on the search query. Here's an example of how you can do this using the useMemo hook in React:
import { useMemo } from 'react';
import { usePage } from '@inertiajs/inertia-react';
const PatientsTable = () => {
const { props } = usePage();
const { patients } = props;
const filteredPatients = useMemo(() => {
// Replace this with your actual filter logic
return patients.filter(patient => patient.name.includes(searchQuery));
}, [patients, searchQuery]);
return (
// Render your table using the filteredPatients data
);
};
Note that you'll need to replace the searchQuery variable with your actual search query value. Also, make sure to import the usePage hook from @inertiajs/inertia-react to access the data passed from the backend.