我是laravel的新手,我需要更新目录,但是我的问题是即使我不想更新程序强制执行的图像..我该怎么办?
我想要的是更新表单而无需任何字段..因为我目前在这里拥有的程序要求我输入所有字段
我的控制器
$this->validate($request, [
'title' => 'required',
'description' => 'required',
'about_image' => ''
]);
if ($request->has('about_image'))
{
//Handle File Upload
$about = [];
foreach ($request->file('about_image') as $key => $file)
{
// Get FileName
$filenameWithExt = $file->getClientOriginalName();
//Get just filename
$filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
//Get just extension
$extension = $file->getClientOriginalExtension();
//Filename to Store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
//Upload Image
$path = $file->storeAs('public/about_images',$fileNameToStore);
array_push($about, $fileNameToStore);
}
$fileNameToStore = serialize($about);
}
else
{
$fileNameToStore='noimage.jpg';
}
foreach ($about as $key => $value) {
$aboutContent = About::find($id);
$aboutContent->title = $request->title[$key];
$aboutContent->description = $request->description[$key];
if($request->hasFile('about_image')){
$aboutContent->about_image = $value;
}
$aboutContent->save();
}
return redirect('/admin/airlineplus/aboutus')->with('success', 'Content Created');
我的视图
{!! Form::open(['action'=>['Admin\AboutusController@update', $about->id], 'method' => 'POST','enctype'=>'multipart/form-data', 'name' => 'add_name', 'id' => 'add_name']) !!}
<div class="form-group">
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<td> {{Form::text('title[]', $about->title, ['class' => 'form-control', 'placeholder' => 'Enter a Title'])}}<br>
{{Form::textarea('description[]', $about->description, ['class' => 'form-control', 'placeholder' => 'Enter a Description'])}} <br>
{{ Form::file('about_image[]') }} <b>{{ $about->about_image }}</b>
</td>
<td>{{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle', 'id'=>'add','name'=>'add', 'style'=>'font-size:15px;']) }}</td>
</tr>
</table>
{{Form::hidden('_method', 'PUT')}}
{{Form::submit('submit', ['class'=>'btn btn-primary', 'name'=>'submit'])}}
</div>
</div>
{!! Form::close() !!}
答案 0 :(得分:0)
如果要更新表单而不需要任何字段,请删除
$this->validate($request, [
'title' => 'required',
'description' => 'required',
'about_image' => ''
]);
来自控制器
if (!empty($about)) {
foreach ($about as $key => $value) {
$aboutContent = About::find($id);
$aboutContent->title = $request->title[$key];
$aboutContent->description = $request->description[$key];
if ($request->hasFile('about_image')) {
$aboutContent->about_image = $value;
}
$aboutContent->save();
}
} else {
$aboutContent = About::find($id);
$aboutContent->title = $request->title[$key];
$aboutContent->description = $request->description[$key];
$aboutContent->save();
}
答案 1 :(得分:0)
您可以删除验证器上的about_image条目,然后在Update方法中检查它是否不为空,就像这样:
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), $this->custom_rules(), $this->custom_messages());
if ($validator->fails()){
return redirect()->route('route_name', $id)->withErrors($validator)->withInput();
}
if ($file = $request->file('about_image')){
$aboutContent->about_image = $file;
}
}
private function custom_rules()
{
$rules = [
'title' => 'required',
'description' => 'required'
];
}
public function custom_messages()
{
return $messages = [
'title.required' => 'Title is required.',
'description.required' => 'Description is required'
];
}