Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

successdav's avatar

Store response return from Inertia.get to a variable in vue

I watch for when the search input changes, then make a get request to laravel that returns members that match the search query. How do I save the response to my members variable.

   let sponsorsearch = ref('');
    let members = ref([]);

    let form = useForm({
                name: null,
                email: null,
                country: 'Nigeria',
                state: null,
                lga: null,
                gender: null,
                title: 'Choose a title',
                mobile: null,
                business_name: null,
                dob: null,
                serial_no: null,
                r_address: null,
                p_address: null,
                working_status: 'Working Status',
                description: null,
                sponsorid: null,
            })

    watch(sponsorsearch, value => {
        Inertia.get('/getsponsors', {search: value}, {
             preserveState: true,
             onFinish: members
        });
    });

Controller method

public function index (Request $request) {
        return Member::where('name', 'like', '%' . $request->search . '%')->get();
    }
0 likes
3 replies
successdav's avatar

Actually, I don't want to rerender the page, I just need this data to populate a select input field. so the user can pick a sponsor and continue with the registration. I have switch to using axios and now axios complain that members is not defined

    let sponsorsearch = ref('');
    let members = ref([]);

    let form = useForm({
                name: null,
                email: null,
                country: 'Nigeria',
                state: null,
                lga: null,
                gender: null,
                title: 'Choose a title',
                mobile: null,
                business_name: null,
                dob: null,
                serial_no: null,
                r_address: null,
                p_address: null,
                working_status: 'Working Status',
                description: null,
                sponsorid: null,
            })

    watch(sponsorsearch, value => {
        // Inertia.get('/getsponsors', {search: value}, {
        //     preserveState: true,
        // });
        
        axios.get('/getsponsors', {params: {search: value}})
        .then((data) => {
            this.members = data.data
        })
    });

The error I get in console

TypeError: Cannot set properties of undefined (setting 'members')

Please or to participate in this conversation.