我尝试了一些代码来将图像存储在数据库中,但是它不起作用。所以我很困惑,因为我是Laravel的初学者。 有人可以帮助我解决我的问题吗?
这是我目前正在做的事情:
// store image
$this->validate($request, [ 'image' => 'image', ]);
if($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
Image:make($image)->resize(145, 170)->save(public_path('/images' . $filename));
$image->save();
};
答案 0 :(得分:0)
$image
不是您的App\Image
模型的实例,而是UploadedFile
类的实例
Image
类是用于图像的PHP类
如果您要存储路径的属性为path
// store image
$this->validate($request, [ 'image' => 'image', ]);
if($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
Image::make($image)->resize(145, 170)->save(public_path('/images' . $filename));
$imageInstance = new \App\Image();
$imageInstance->path = '/images' . $filename;
$imageInstance->save();
};
答案 1 :(得分:0)
此示例显示了如何上传多张图片
//查看
<form class="form-horizontal" action="{{ url('/picture-update') }}" method="post" role="form" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="col-sm-4"><label>Profile picture: </label>
<div class="form-group">
<input type="file" name="filename[]" required>
<br>
</div>
<button type="submit" class="btn btn-success btn-xs">Upload</button>
</form>
//控制器
public function uploadAttachment(Request $request)
{
$this->validate($request, [
'filename.*' => 'mimes:pdf,doc,docx,jpeg,jpg,gif,png,bmp|max:2048',
//processing insert into attachment table
if($request->hasfile('filename'))
{
foreach($request->file('filename') as $file)
{
$name=$file->getClientOriginalName();
$file->move(public_path().'/../../yourdomain.com/attachments_files', $name);
DB::table('tblstaffAttachment')->insert([
'filepath' => $name,
]);
}
}
return back()->with('message', 'File Uploaded!');
}
//路线
Route::post('/picture-update','YourController@uploadAttachment');