图片未存储在数据库中
我已经尝试过寻找其他可能的解决方案,但是使用guessextension
public function store(Request $request)
{
$student = new student();
$this->validateRequest();
$student->name = $request->name;
$student->address = $request->address;
if ($request->hasFile('image')) {
$image = $request->file('image');
$extension = $file->getClientOriginalExtension();
$filename=time() .'.'. $extension;
$file->move('uploads/student/',$filename);
$student->image = $filename;
}
else {
return $request;
$student->image ='';
}
$student->save();
return redirect()->route('show')->with('response', 'Registered Successfully');
}
这是表格
@extends('layouts.app')
@section('content')
<div class="container">
<form method="get" action="/student" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="email">Name:</label>
<input type="text" class="form-control" name="name" id="email">
</div>
<div class="form-group">
<label for="pwd">Address:</label>
<input type="text" class="form-control" name="address" id="email">
</div>
<div class="custom-file">
<input type="file" name="image" class="custom-file-input" >
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
@stop
我需要的是要存储在数据库中的图像。
答案 0 :(得分:2)
实际上,当您将图像上传到服务器时,仅将图像的路径保存到数据库中
因此,首先您需要创建一个从"public/storage"
到
"storage/app/public"
通过工匠命令:php artisan storage:link
之后,您可以从
中优化代码的这一部分。if ($request->hasFile('image')) {
$image = $request->file('image');
$extension = $file->getClientOriginalExtension();
$filename=time() .'.'. $extension;
$file->move('uploads/student/',$filename);
$student->image = $filename;
}
这个简单的
if ($request->hasFile('image')) {
$image = $request->image->store('uploads');
}