当我在Vue laravel中使用文件上传更新另一个文件时,如何保持文件的当前值?

b0zn9rqh  于 2023-01-05  发布在  Vue.js
关注(0)|答案(1)|浏览(132)

我正在尝试编辑具有2个上载文件的表单
这两个都是NULL(如果我在创建过程中没有上传任何文件)
因此,如果我是第一次创建并上传这些文件,假设我上传了一个文件,而另一个文件为空,则数据库中将显示“file 1”具有值,“file 2”为NULL,因此到目前为止一切正常
但是,当我试图编辑,让我们说,我想上传一个文件,现在为“file 2”
它将上载文件,但如果我没有为“file 1”提供任何值,则更新后它将为NULL
以下是观点:

<tab-content title="Tab B" :before-change="validationFormInfo">
    <validation-observer ref="infoRules" tag="form">
      <b-row>
        <b-col md="6">

          <b-form-group label="Parties IC" label-for="opartiesic">
            <b-form-file
              v-model="matter.file1"
              name="file1"
              placeholder="Choose a file or drop it here..."
              drop-placeholder="Drop file here..."
              v-on:change="onChange1"
            />

            <b-card-text class="my-1">
              Selected file: <strong>{{ matter.file1 ? matter.file1.name : "" }}</strong>
            </b-card-text>
            <b-card-text class="my-1">
                Current file:
                <strong>{{ matter.parties_ic }}</strong>
              </b-card-text>
          </b-form-group>

          <b-form-group label="Company Forms" label-for="compforms">
            <b-form-file
              v-model="matter.file2"
              name="file2"
              placeholder="Choose a file or drop it here..."
              drop-placeholder="Drop file here..."
              v-on:change="onChange2"
            />

            <b-card-text class="my-1">
              Selected file: <strong>{{ matter.file2 ? matter.file2.name : "" }}</strong>
            </b-card-text>
            <b-card-text class="my-1">
                Current file:
                <strong>{{ matter.company_forms }}</strong>
              </b-card-text>
          </b-form-group>

        </b-col>
      </b-row>
    </validation-observer>
  </tab-content>

脚本:

data() {
return {
    matter:{
        file1: "",
        file2: "",
        _method: "patch"
    }
};
},
methods: {
onChange(e) {
  this.matter.file1 = e.target.files[0];
},
onChange2(e) {
  this.matter.file2 = e.target.files[0];
},
formSubmit(e) {
            e.preventDefault();
            let existingObj = this;
            const config = {
                headers: {
                    'content-type': 'multipart/form-data'
                }
            }
            let data = new FormData();
            data.append("file1", this.matter.file1);
            data.append("file2", this.matter.file2);
            data.append('_method', this.matter._method);
            axios.post(`/api/auth/matter-dispute/${this.$route.params.id}`, data, config)
                .then(function (res) {
                    existingObj.success = res.data.success;
                })
                .catch(function (err) {
                    existingObj.output = err;
                });
                this.$router.push({name:"matter-management"})
        },
}
}

控制器(更新功能)

public function update(Request $request, matter_dispute $matter_dispute)
{

    if ($request->hasFile('file1')) {
        $file_name = time().'_'.$request->file1->getClientOriginalName();
        $file_path = $request->file('file1')->move(public_path('uploads/MatterDispute/'. $matter_dispute->id), $file_name);
        $request->parties_ic = time().'_'.$request->file1->getClientOriginalName();
        $request->parties_ic_path = '/storage/' . $file_path;
    }
    if ($request->hasFile('file2')) {
        $file_name = time().'_'.$request->file2->getClientOriginalName();
        $file_path = $request->file('file2')->move(public_path('uploads/MatterDispute/'. $matter_dispute->id), $file_name);
        $request->company_forms = time().'_'.$request->file2->getClientOriginalName();
        $request->company_forms_path = '/storage/' . $file_path;
    }
    DB::table('matter_disputes')
    ->join('matter_dispute_bs', 'matter_disputes.id', '=', 'matter_dispute_bs.matter_disputes_id')
    ->join('matter_dispute_cs', 'matter_disputes.id', '=', 'matter_dispute_cs.matter_disputes_id')
    ->where('matter_disputes.id',$matter_dispute->id)
    ->update([
        'parties_ic' => $request->parties_ic,
        'company_forms' => $request->company_forms,
    ]);
}

所以我假设在请求中没有任何内容的情况下,该值将为NULL,因为在-〉update查询中它从请求中获取值
那么如果它没有改变,我怎么保持当前值呢?

e3bfsja2

e3bfsja21#

溶液:
向函数添加更多DB::table()子句,并指定要更新的模型和相应数据。
例如,如果要更新matter_dispute_bs和matter_dispute_cs模型中的数据,可以执行以下操作:

public function update(Request $request, matter_dispute $matter_dispute)
{
// Update data in the matter_disputes table
DB::table('matter_disputes')
->where('id', $matter_dispute->id)
->update([
    'column_1' => $request->column_1,
    'column_2' => $request->column_2,
    // other columns you want to update in the matter_disputes table
]);

// Update data in the matter_dispute_cs table
DB::table('matter_dispute_cs')
->where('matter_disputes_id', $matter_dispute->id)
->update([
    'column_1' => $request->column_1,
    'column_2' => $request->column_2,
    // other columns you want to update in the matter_dispute_cs table
]);
}

if ($request->hasFile('file1')) {
    $file_name = time().'_'.$request->file1->getClientOriginalName();
    $file_path = $request->file('file1')->move(public_path('uploads/MatterDispute/'. $matter_dispute->id), $file_name);
    $request->parties_ic = time().'_'.$request->file1->getClientOriginalName();
    $request->parties_ic_path = '/storage/' . $file_path;
        DB::table('matter_dispute_bs')
    ->where('matter_disputes_id', $matter_dispute->id)
    ->update([
    'parties_ic' => $request->parties_ic,
    'parties_ic_path' => $request->parties_ic_path,
]);
}

相关问题