嗨,我是Laravel的新手,我已经完成了将图像上传到路径 img / banner / {{此处上传图片}}
我的疑问是将图像路径保存在数据库中,
用户上传
在检索数据时如何在此处提供我的URL路径。
我已经创建了一个数据库名称上传文件
id user_id group_id文件扩展名文件大小位置created_at Updated_at
刀片文件
<div class="panel panel-primary">
<div class="panel-heading"><h2>Slide Image Upload</h2></div>
<div class="panel-body">
@if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
<img src="/img/banner/{{ Session::get('image') }}">
@endif
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('image.upload.post') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-md-6">
<input type="file" name="image" class="form-control">
</div>
<div class="col-md-6">
<button type="submit" class="btn btn-success">Upload</button>
</div>
</div>
</form>
</div>
</div>
控制器:
public function imageUploadPost()
{
request()->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$imageName = time().'.'.request()->image->getClientOriginalExtension();
request()->image->move(public_path('img/banner'), $imageName);
return back()
->with('success','You have successfully upload image.')
->with('image',$imageName);
}
路线:
Route::post('/imageupload', 'Admin\ImageController@imageUploadPost')->name('image.upload.post');
任何帮助将不胜感激。
答案 0 :(得分:1)
我希望您已经包含名称空间,如果没有的话
use DB;
$full_path = public_path('img/banner');
$ext = request()->image->getClientOriginalExtension();
$size = request()->image->getSize();
DB::table('uploads')->insert(
['group_id' => '1', user_id' => '1', 'filename' => $imageName,'filesize'=>$size ,'extension'=> $ext,'location' => $full_path,'created_at'=> date('Y-m-d H:m:s')]
);
由此可以获取数据库值,$ imagename是用于上传图像的名称,不确定您的group_id是否设置为1。
最新修改
这是获取imageName的方法:
$imageName = time().'.'.request()->image->getClientOriginalExtension();
仅修饰代码