Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

i have a question.

database settings table : enter image description here

When adding a new setting I am sending data like this :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

           <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>

and I’m querying this while editing:

              @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

update method:

 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.');
}

my question is: if value part is photo how can i update it as photo…

>Solution :

You can use the UploadedFile‘s store or storeAs to store the uploaded file and then store the file path as value in the database.

And don’t forget to have enctype="multipart/form-data" on the form

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading