我有一个包含多个字段的表格如下。
当我更新表单时,只更新 large_image
字段, thumbnail_image
不会更新。
我的表单
<form action="/update', $id)}}" enctype="multipart/form-data" method="post" files="true">
<input type="text" name="name">
<input type="file" name="large_image" multiple="false" accept="image/x-png, image/jpeg">
<input type="file" name="thumb_image" multiple="false" accept="image/x-png, image/jpeg">
<input type="submit">
模型
class Item extends Model {
protected $fillable = ['name', 'large_image', 'thumb_image'];
}
控制器
public function update((Request $request, $id){
$requestData = $request->all();
$item= Item::findOrFail($id);
if ($request->hasFile('large_image'))
{
$fileName = $request->file('large_image')->getClientOriginalName();
$fileExt = $request->file('large_image')->getClientOriginalExtension();
$fullFileName = $fileName.'_'.time().'.'.$fileExt;
$path = public_path('images');
Image::make($fileName)->save($path . DIRECTORY_SEPARATOR. $fullFileName);
$requestData['large_image'] = "images/{$fullFileName}";
}
if ($request->hasFile('thumb_image'))
{
$file = $request->file('thumb_image');
{
$file_thumb = $request->file('thumb_image')->getClientOriginalName();
$file_thumb_Ext = $request->file('thumb_image')->getClientOriginalExtension();
$fullfile_thumb = $file_thumb.'_'.time().'.'.$filethumb_Ext;
$path_thumb = public_path('images/thumb');
Image::make($file)->save($path_thumb . DIRECTORY_SEPARATOR. $fullfile_thumb);
}
$requestData['thumb_image'] = "images/thumb/{$fullfile_thumb}";
}
$item->update($requestData);
}
我在这里缺少什么?但是上传了两个图像,并且在create方法中将图像路径写入数据库。
答案 0 :(得分:1)
<强>模型强>
将 thumb_image 替换为 thumbnail_image ,因为表 thumbnail_image <中有列名/ EM> 强>
class Item extends Model {
protected $fillable = ['name', 'large_image', 'thumbnail_image'];
}
<强>控制器强>
在控制器中用 thumb_image 替换键值 thumb_image ,因为表格中的列名已经很强> thumbnail_image 强>
替换
$的RequestData [&#39; thumb_image&#39;]
通过
$的RequestData [&#39; thumbnail_image&#39;]
public function update((Request $request, $id){
$requestData = $request->all();
$item= Item::findOrFail($id);
if ($request->hasFile('large_image'))
{
$fileName = $request->file('large_image')->getClientOriginalName();
$fileExt = $request->file('large_image')->getClientOriginalExtension();
$fullFileName = $fileName.'_'.time().'.'.$fileExt;
$path = public_path('images');
Image::make($fileName)->save($path . DIRECTORY_SEPARATOR. $fullFileName);
$requestData['large_image'] = "images/{$fullFileName}";
}
if ($request->hasFile('thumb_image'))
{
$file = $request->file('thumb_image');
{
$file_thumb = $request->file('thumb_image')->getClientOriginalName();
$file_thumb_Ext = $request->file('thumb_image')->getClientOriginalExtension();
$fullfile_thumb = $file_thumb.'_'.time().'.'.$filethumb_Ext;
$path_thumb = public_path('images/thumb');
Image::make($file)->save($path_thumb . DIRECTORY_SEPARATOR. $fullfile_thumb);
}
$requestData['thumbnail_image'] = "images/thumb/{$fullfile_thumb}";
}
$item->update($requestData);
}
希望这对你有用。
答案 1 :(得分:0)
你的模特是:
class Item extends Model {
protected $fillable = ['name', 'large_image', 'thumb_image'];
}
现在查看您的数据库,列名称不同,因此模型应如下所示:
class Item extends Model {
protected $fillable = ['name', 'large_image', 'thumbnail_image'];
}
祝你好运!