简体   繁体   English

Vue/Inertia.js 不更新数据库中的值

[英]Vue/Inertia.js not updating value in database

I have the below form:我有以下表格:

<form @change="updatePipeline">
  <select v-model="pipeline.input_mode">
     <option value="text">Text</option>
     <option value="html">HTML</option>
     <option value="json">JSON</option>
  </select>
</form>

The @change is calling the below: @change调用如下:

<script setup>
import {useForm} from "@inertiajs/inertia-vue3";
const props = defineProps({
    pipeline: {
        type: Object,
        required: true,
    },
});

const form = useForm({
    input_mode: props.pipeline.input_mode,
});

const updatePipeline = () => {
    form.put(route('pipelines.update', props.pipeline), {
        errorBag: 'updatePipeline',
        preserveScroll: true,
    });
};

The value is not updated in the database:该值未在数据库中更新:

public function update(Request $request, Pipeline $pipeline)
{
    $pipeline->update(
        $request->validate([
            'input_mode' => 'required|string|min:3|max:255',
        ])
    );


    return redirect()
        ->route('pipelines.edit', ['pipeline' => $pipeline])
        ->with('success', 'Pipeline edited successfully.');
}

What am I doing wrong?我究竟做错了什么?

PAY ATTENTION TO THE FIRST COMMENT ON THIS ANSWER.请注意对此答案的第一条评论。 IT DEPENDS ON THE VERSION它取决于版本

I'm not an expert in inertia but when you do我不是惯性专家,但当你这样做时

$pipeline->update(
        $request->validate([
            'input_mode' => 'required|string|min:3|max:255',
        ])
    );

it returns a void as you can see here https://laravel.com/api/5.5/Illuminate/Validation/Validator.html#method_validate它返回一个void ,您可以在这里看到https://laravel.com/api/5.5/Illuminate/Validation/Validator.html#method_validate

What you are looking for is:您正在寻找的是:

$request->validate(['input_mode' => 'required|string|min:3|max:255'])

$pipeline->update($request->validated());

validated() will return an array of items that were validated. validated()将返回一个经过验证的项目数组。


EDIT编辑

Do you have the input_mode in the $fillables in your modal Pipeline ?您的模态Pipeline中的$fillables fillables 中有input_mode吗? If not, that's your problem如果没有,那是你的问题

protected $fillables = ['input_mode'];

So I actually figured this out shortly after asking the question here.所以我在这里问这个问题后不久就发现了这一点。 The reason why the database is not being persisted, is that I am referring pipeline on the v-model property.数据库没有被持久化的原因是我指的是v-model属性上的pipeline Instead, I should refer to form :相反,我应该参考form

<form @change="updatePipeline">
  <select v-model="form.input_mode">
     <option value="text">Text</option>
     <option value="html">HTML</option>
     <option value="json">JSON</option>
  </select>
</form>

So, instead of having: v-model="pipeline.input_mode" , I have: v-model="form.input_mode" .所以,我没有: v- v-model="pipeline.input_mode" ,而是: v-model="form.input_mode"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM