我想将图像上传到laravel中的公共(public)文件夹和数据库,但我遇到了一些问题,图像正在上传到公共(public)文件夹,但图像没有保存到数据库中。
谁能帮我告诉我哪里出了问题?这是我的代码。
Controller
public function clientaction(Request $request)
{
$validation = validator::make($request->all(),[
'select_file' =>'required|image|mimes:jpeg,png,jpg,gif|max:2048'
]);
if($validation->passes())
{
$image = $request->file('select_file');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
return response()->json([
'message' => 'Image Upload Successfully',
'class_name' => 'alert-success'
]);
}
else
{
return response()->json([
'message' => $validation->errors()->all(),
'class_name' => 'alert-danger'
]);
}
DB::table('logo-clients')->insert([
'select_file'=> $new_name
]);
}
路线
Route::get('DashBoard','AdminController@DashBoard');
Route::get('Clients','AdminController@clients');
Route::post('/ajax_upload/action','AdminController@clientaction')->name('ajaxupload.action');
最佳答案
因为你已经返回了值,这意味着之后的操作是无用的
所以你只需要移动它
DB::table('logo-clients')->insert([
'select_file'=> $new_name
]);
在这部分下面
$image->move(public_path('images'), $new_name);
//move it into here
关于php - 我想将图像上传到 laravel 中的公共(public)文件夹和数据库,但我遇到了一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58668304/