i have a question.
When adding a new setting I am sending data like this :
<div >
<label>Tip</label>
<select 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 >
<img width="200" height="100" src="{{ asset('storage/' . $setting->value) }}">
</div>
<div >
<input type="file" name="value" />
</div>
@else
<div >
<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...
CodePudding user response:
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
CodePudding user response:
**This is how I solved my problem, it was a bit of a simple question, sorry **
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.');
}