我试图将数据插入数据库,同时将图像上传到路径。看来问题出在验证上,但我看不到错误在哪里。我被困了两天,试图弄清楚我在做什么错。
以下是上传代码:
protected function store(Request $request)
{
$nameFile = null;
if ($request->hasFile('image')) {
$name = uniqid(date('HisYmd'));
$extension = $request->image->extension();
$nameFile = "{$name}.{$extension}";
$upload = $request->image->storeAs('categories', $nameFile);
return $namefile;
}
}
这是创建函数:
protected function create(array $data){
$image = $this->store();
return Bottleneck::create([
'setor_id' => $data['setor_id'],
'mes' => $data['mes'],
'ano' => $data['ano'],
'andon' => $data['andon'],
'conteudo' => $data['coteudo'],
'atividade_total' => $data['atividade_total'],
'image' => $image
]);
}
这是验证:
protected function validator(array $data){
return Validator::make($data , [
'setor_id' => ['required', 'integer', 'exists:setor_linha,id'],
'mes' => ['required', 'integer'],
'ano' => ['required', 'integer'],
'andon' => ['required', 'string'],
'conteudo' => ['required', 'string'],
'atividade_total' => ['required', 'string'],
'image' => ['image'],
]);
}
保存功能:
public function save(Request $request){
$validation = $this->validator($request->all());
if($validation->fails()){
Session::flash('message', 'Error');
return redirect()->route('bottleneck');
}else{
$bottleneck = $this->create($request->all());
Session::flash('message', 'Success');
return redirect()->route('bottleneck');
}
}
这是我的表格:
<form method="POST" action="{{route("create.bottleneck")}}">
@csrf
<div class="form-group row">
<label for="setor_id" class="col-md-4 col-form-label text-md-right">
{{__('Production Line')}}
</label>
<div class="col-md-8">
<select id="setor_id" name="setor_id" class="form-control " required>
<option>{{__('Select Value')}}</option>
@forEach($line as $data)
<option value={{$data->id}}>{{$data->name}}</option>
@endforEach
</select>
@if($errors->has('setor_id'))
<span class="invalid-feedback" role="alert">
<strong>
{{$errors->first('setor_id')}}
</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="mes" class="col-md-4 col-form-label text-md-right">{{__('Month')}}</label>
<div class="col-md-8">
<input type="number" name="mes" class="form-control">
@if($errors->has('mes'))
<span class="invalid-feedback" role="alert">
<strong>
{{$errors->first('mes')}}
</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="ano" class="col-md-4 col-form-label text-md-right">{{__('Year')}}</label>
<div class="col-md-8">
<input type="number" name="ano" class="form-control" required>
@if($errors->has('ano'))
<span class="invalid-feedback" role="alert">
<strong>
{{$errors->first('ano')}}
</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="andon" class="col-md-4 col-form-label text-md-right">{{__('Andon')}}</label>
<div class="col-md-8">
<textarea name="andon" class="form-control" required></textarea>
@if($errors->has('andon'))
<span class="invalid-feedback" role="alert">
<strong>
{{$errors->first('andon')}}
</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="conteudo" class="col-md-4 col-form-label text-md-right">{{__('Content')}}</label>
<div class="col-md-8">
<textarea name="conteudo" class="form-control" required></textarea>
@if($errors->has('conteudo'))
<span class="invalid-feedback" role="alert">
<strong>
{{$errors->first('conteudo')}}
</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="atividade_total" class="col-md-4 col-form-label text-md-right">{{__('Total Work')}}</label>
<div class="col-md-8">
<input type="text" name="atividade_total" required>
@if($errors->has('atividade_total'))
<span class="invalida-feedback" role="alert">
<strong>
{{$errors->first('atividade_total')}}
</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="image" class="col-md-4 col-form-label text-md-right">{{__('Image')}}</label>
<div class="col-md-8">
<input type="file" name="image" class="form-control">
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{__('Save')}}
</button>
</div>
</div>
</form>
答案 0 :(得分:0)
您的表单需要具有enctype="multipart/form-data"
属性才能上传图像。