Laravel API Eloquent Vue 中的 Where 子句不起作用

Laravel API Eloquent Where Clause with Vue not working

我的问题是,为什么我的 where 子句不起作用? 我为我的 vue(使用 vuex)项目使用 Laravel API。

这是控制器函数

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

我也使用 vuetify,这就是我如何获得 client_id :

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

我的 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);
                });
         },

我的API路线:

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

当我控制台记录“cnr”时,我得到了正确的 ID,但没有得到任何数据。如果我在 where 子句中替换 $id,我会得到正确的信息。我觉得这是我在某个地方犯的一个愚蠢的错误,但这就是我来这里的目的。

在你的axios请求中,你需要标记id参数。

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

应该是

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