我正在用laravel上传图像,该图像也保存在数据库和项目文件夹中。我正在使用当前时间戳重命名图像名称。但我想使用该特定表的字段名称重命名。我怎么做? 这是我的控制器功能。
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if ($validator->passes()) {
$formInput = $request->except('image');
//validation
$image = $request->image;
if($image){
$imageName = time().'.'.$request->image->getClientOriginalExtension();
$image->move('images/employees',$imageName);
$formInput['image'] = $imageName;
}
Employee::create($formInput);
return redirect()->to('/employeesedit');
}
}
答案 0 :(得分:0)
我建议您这样做:
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if ($validator->passes()) {
$formInput = $request->except('image');
//validation
$image = $request->image;
if($image){
// replace $formInput['name'] with the relevant field name
$imageName = str_slug($formInput['field_name']).'.'.$request->image->getClientOriginalExtension();
$image->move('images/employees',$imageName);
$formInput['image'] = $imageName;
}
Employee::create($formInput);
return redirect()->to('/employeesedit');
}
}