Home > Blockchain >  Laravel API Eloquent Where Clause with Vue not working
Laravel API Eloquent Where Clause with Vue not working

Time:11-16

My question is, why doesn't my where clause work? I use a Laravel API for my vue (with vuex) project.

Here's the Controller function

    public function specific_client(Request $request) {
            $id = $request->id;
            return JsonResource::collection(
            Measurement::where('client_id', '=',$id)
            ->with(['clients', 'measurement_data'])->get());
    }

I also use vuetify, this is how I get the client_id :

<v-select v-model="cnr" :items="clients" item-text="clientnumber" item-value="id" :hint="cnr" solo></v-select>

My store.js :

        fetchClientMeasurements({commit}, cnr) {
            axios.post("http://localhost:8000/api/clientnr", cnr)
                .then(response => {
                    console.log(response.data.data);
                    console.log(cnr);
                    commit("setMeasurements", response.data.data);
                });
         },

My API Route :

Route::post('clientnr', [MeasurementController::class, 'specific_client']);

When I console log "cnr" I get back the correct ID but I don't get any data back. If I replace $id in the where clause I do get back the correct information. I feel like it is a stupid mistake I made somewhere, but that's what I'm here for.

CodePudding user response:

In your axios request, you need to label the id parameter.

axios.post("http://localhost:8000/api/clientnr", cnr)

should be

axios.post("http://localhost:8000/api/clientnr", {id: cnr})

  • Related