如果是照片,如何更新传入值? [LARAVEL 9]

How do I update the incoming value if it's a photo? [LARAVEL 9]

我有一个问题。

数据库设置table:

添加新设置时,我发送的数据是这样的:

           <div class="form-group">
                <label>Tip</label>
                <select class="form-control" name="type">
                    <option value="text">Kısa yazı</option>
                    <option value="file">Dosya (Resim vb.)</option>
                    <option value="textarea">Uzun yazı</option>
                    <option value="ckeditor">Detaylı Metin Editörü</option>
                </select>
            </div>

我在编辑时查询这个:

              @if ($setting->type == 'file')
                    <div class="form-group">
                <img width="200" height="100" src="{{ asset('storage/' . $setting->value) }}">                   
                    </div>
                    <div class="form-group">
                        <input type="file" name="value" class="form-control" />
                    </div>
                @else
                    <div class="form-group">
                        <label>Açıklama:</label>
                @endif

更新方法:

 public function update(Request $request, Setting $setting)
{
    $request->validate([
        'key' => 'required',
        'value' => 'required',
        'type' => 'required',
        'description' => 'required',
    ]);




    $setting->update([
        'key' => $request->key,
        'value' => $request->value,
        'type' => $request->type,
        'description' => $request->description,

    ]);




    return redirect()->route('admin.settings.index')->with('message', 'Ayar başarıyla güncellendi.');
}

我的问题是:如果值部分是照片,我如何将其更新为照片...

**这就是我解决问题的方法,这是一个有点简单的问题,抱歉 **

    public function update(Request $request, Setting $setting)
{
    $request->validate([
        'key' => 'required',
        'type' => 'required',
        'description' => 'required',
    ]);

    $value = $setting->value;
    if ($request->type == 'file') {
        $value = $request->file('value')->store('public/settings');
    } else {
        $value = $request->value;
    }

    $setting->update([
        'key' => $request->key,
        'type' => $request->type,
        'description' => $request->description,
        'value' => $value,
    ]);

    return redirect()->route('admin.settings.index')->with('message', 'Ayar başarıyla güncellendi.');
}

可以使用UploadedFilestore或者storeAs来存储上传的文件,然后存储文件路径作为数据库中的

别忘了在表格上填写 enctype="multipart/form-data"

public function update(Request $request, Setting $setting)
{
    $validated = $request->validate([
        'key' => 'required',
        'value' => 'required',
        'type' => 'required',
        'description' => 'required',
    ]);

    if($request->hasFile('value') && $request->file('value')->isValid() ){
        $validated['value'] = $request->file('value')->store('/files', 'public');
    }


    $setting->update($validated);




    return redirect()->route('admin.settings.index')->with('message', 'Ayar başarıyla güncellendi.');
}

Laravel Docs - Request - Files