如何更新 Laravel 5.2 中的复选框字段和选项字段?
How to update checkbox field and option field in Laravel 5.2?
我可以更新 form
的 text
字段,但我无法更新 checkbox
字段、option
字段和 [=30] 中的 file
字段=].我要更新,然后我可以看到所有文本值都完美显示在更新 blade 视图中,但在选项字段、复选框字段中我看到它是默认值,它不是来自数据库。再次,如果我用另一个选项更新,那么它不是 saving.What 我到目前为止已经尝试过:
我的控制器:
public function update(Request $request, $id=0)
{
//
$id = $request->input("id");
$product = Product::find($id);
$product->product_name = $request->input('product_name');
$product->product_storage = $request->input('product_storage');
$product->product_percentage = $request->input('product_percentage');
$product->can_draw = $request->input('can_draw');
$product->real_price = $request->input('real_price');
$product->product_image = $request->input('product_image');
$product->save();
$request->session()->flash('alert-info', 'Product Successfully Updated!');
return Redirect::to('admin/product/all');
}
我的 update.blade.php 查看:
<div class="form-group">
<label class="col-md-3 control-label">{{ trans('common.can_be_draw') }}</label>
<div class="col-md-9">
<div class="input-icon">
<i class="fa fa-money" aria-hidden="true"></i>
<select name="can_draw" class="form-control">
<option value="{{ $product->can_draw }}">{{ trans('common.open') }}open</option>
<option value="{{ $product->can_draw }}">{{ trans('common.close') }}close</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">{{ trans('common.real_prize') }}</label>
<div class="col-md-9">
<div class="input-icon">
<input type="radio" class="form-control" name="real_price" value="{{ $product->real_price }}"> {{ trans('common.yes') }}yes
<input type="radio" class="form-control" name="real_price" value="{{ $product->real_price }}" checked="checked"> {{ trans('common.no') }}no
</div>
</div>
</div>
<div class="form-group">
<label for="exampleInputFile" class="col-md-3 control-label">{{ trans('common.product_picture') }}</label>
<div class="col-md-9">
<input type="file" id="exampleInputFile" name="product_image" value="{{ $product->product_image }}">
<small>{{ trans('common.pic_summery') }}</small>
</div>
</div>
您的选项字段具有相同的值 ({{ $product->can_draw }})
<option value="value1"{{( $product->can_draw=='value1'?selected)}}>{{ trans('common.open') }}open</option>
<option value="value2 " {{( $product->can_draw=='value1'?selected)}}>{{ trans('common.close') }}close</option>
并且文件字段必须使用文件而不是输入:
$product->product_image = $request->file('product_image');
您只需通过 select 名称请求即可保存选项值。
在您的示例中:
select name = "can_draw"
只需保存请求中的值:
$product->can_draw = $request->can_draw;
对于相同的复选框:
$product->real_price = $request->real_price;
不需要在请求中指明 input() 助手。
要检索选项和复选框值的所有值,请使用 foreach 方法:
@foreach($can_draws as $can_draw)
<option value="{{ $can_draw->id }}">{{ $can_draw->name}}</option>
@endforeach
只需使用这个:
<input type="radio" {{$product->can_draw == 'Yes' ? 'checked' : ''}} class="form-control" name="real_price" value="Yes"> {{ trans('common.yes') }}yes
<input type="radio" {{$product->can_draw == 'Yes' ? '' : 'checked'}} class="form-control" name="real_price" value="No" > {{ trans('common.no') }}no
EDIT
我已经编辑了以上内容*
您应该将值更改为 Yes 和 No,并使用它来检查保存的 value
是 Yes
还是 No
。 real_price
多少并不重要
我可以更新 form
的 text
字段,但我无法更新 checkbox
字段、option
字段和 [=30] 中的 file
字段=].我要更新,然后我可以看到所有文本值都完美显示在更新 blade 视图中,但在选项字段、复选框字段中我看到它是默认值,它不是来自数据库。再次,如果我用另一个选项更新,那么它不是 saving.What 我到目前为止已经尝试过:
我的控制器:
public function update(Request $request, $id=0)
{
//
$id = $request->input("id");
$product = Product::find($id);
$product->product_name = $request->input('product_name');
$product->product_storage = $request->input('product_storage');
$product->product_percentage = $request->input('product_percentage');
$product->can_draw = $request->input('can_draw');
$product->real_price = $request->input('real_price');
$product->product_image = $request->input('product_image');
$product->save();
$request->session()->flash('alert-info', 'Product Successfully Updated!');
return Redirect::to('admin/product/all');
}
我的 update.blade.php 查看:
<div class="form-group">
<label class="col-md-3 control-label">{{ trans('common.can_be_draw') }}</label>
<div class="col-md-9">
<div class="input-icon">
<i class="fa fa-money" aria-hidden="true"></i>
<select name="can_draw" class="form-control">
<option value="{{ $product->can_draw }}">{{ trans('common.open') }}open</option>
<option value="{{ $product->can_draw }}">{{ trans('common.close') }}close</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">{{ trans('common.real_prize') }}</label>
<div class="col-md-9">
<div class="input-icon">
<input type="radio" class="form-control" name="real_price" value="{{ $product->real_price }}"> {{ trans('common.yes') }}yes
<input type="radio" class="form-control" name="real_price" value="{{ $product->real_price }}" checked="checked"> {{ trans('common.no') }}no
</div>
</div>
</div>
<div class="form-group">
<label for="exampleInputFile" class="col-md-3 control-label">{{ trans('common.product_picture') }}</label>
<div class="col-md-9">
<input type="file" id="exampleInputFile" name="product_image" value="{{ $product->product_image }}">
<small>{{ trans('common.pic_summery') }}</small>
</div>
</div>
您的选项字段具有相同的值 ({{ $product->can_draw }})
<option value="value1"{{( $product->can_draw=='value1'?selected)}}>{{ trans('common.open') }}open</option>
<option value="value2 " {{( $product->can_draw=='value1'?selected)}}>{{ trans('common.close') }}close</option>
并且文件字段必须使用文件而不是输入:
$product->product_image = $request->file('product_image');
您只需通过 select 名称请求即可保存选项值。
在您的示例中:
select name = "can_draw"
只需保存请求中的值:
$product->can_draw = $request->can_draw;
对于相同的复选框:
$product->real_price = $request->real_price;
不需要在请求中指明 input() 助手。
要检索选项和复选框值的所有值,请使用 foreach 方法:
@foreach($can_draws as $can_draw)
<option value="{{ $can_draw->id }}">{{ $can_draw->name}}</option>
@endforeach
只需使用这个:
<input type="radio" {{$product->can_draw == 'Yes' ? 'checked' : ''}} class="form-control" name="real_price" value="Yes"> {{ trans('common.yes') }}yes
<input type="radio" {{$product->can_draw == 'Yes' ? '' : 'checked'}} class="form-control" name="real_price" value="No" > {{ trans('common.no') }}no
EDIT
我已经编辑了以上内容*
您应该将值更改为 Yes 和 No,并使用它来检查保存的 value
是 Yes
还是 No
。 real_price