【问题标题】:Laravel API Eloquent Where Clause with Vue not workingLaravel API Eloquent Where 子句与 Vue 不工作
【发布时间】:2021-12-27 09:44:21
【问题描述】:

我的问题是,为什么我的 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.post("http://localhost:8000/api/clientnr", cnr) 应该是axios.post("http://localhost:8000/api/clientnr", {id: cnr})
  • 哇,这很有意义(我超级累哈哈)。非常感谢!如果您将其写为答案,我将投票并设置为答案。 :)

标签: sql laravel vue.js eloquent vuex


【解决方案1】:

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

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

应该是

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-06
    • 2022-10-08
    • 2016-03-02
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多