i have a question.
When adding a new setting I am sending data like this :
<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…
Advertisement
Answer
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.'); }