gpt4 book ai didi

php - 如何在默认情况下选中复选框并从 Laravel 中的数据库中检索旧值

转载 作者:行者123 更新时间:2023-12-02 07:27:20 25 4
gpt4 key购买 nike

我有一个复选框,我想让我的复选框默认选中,如果用户取消选中复选框并保存表单,我需要在检索或编辑表单数据时取消选中该复选框。

我的查看页面

<div class="form-group {{ $errors->has('active') ? 'error' : '' }}">
<label for="active" class="col-md-3 control-label">@lang('admin/assetdetails/form.active')</label>
<div class="controls col-md-7">
{{ Form::checkbox('active', 1, Input::old('active', $assetdetail->active), array('class'=>'isnesdbox')) }}
{{ $errors->first('active', '<span class="alert-msg"><i class="icon-remove-sign"></i> :message</span>') }}
</div>
</div>

给这个正确地从数据库中带回旧数据,但我无法将我的复选框选中为默认

Controller :
public function postEdit($assetdetailId = null)
{
// Check if the location exists
if (is_null($assetdetail = Assetdetail::find($assetdetailId)))
{
// Redirect to the blogs management page
return Redirect::to('admin/settings/assetdetails')->with('error', Lang::get('admin/assetdetails/message.does_not_exist'));
}



// get the POST data
$new = Input::all();

// attempt validation
if ($assetdetail->validate($new))
{

// Update the location data

$assetdetail ->asset_number = e(Input::get('asset_number'));
$assetdetail ->location_id = e(Input::get('location_id'));
$assetdetail ->assign_to = e(Input::get('assign_to'));
$assetdetail ->asset_type_id = e(Input::get('asset_type_id'));
$assetdetail ->nesd = e(Input::get('nesd'));
$assetdetail ->active = e(Input::get('active'));
$assetdetail ->shift = e(Input::get('shift'));
$assetdetail ->supplier_name = e(Input::get('supplier_name'));
$assetdetail ->description = e(Input::get('description'));
$assetdetail ->dateof_purchase = e(Input::get('dateof_purchase'));
$assetdetail ->label_number = e(Input::get('label_number'));
$assetdetail ->purchase_price = e(Input::get('purchase_price'));
$assetdetail ->dateof_disposed = e(Input::get('dateof_disposed'));
$assetdetail ->depreciation_type = e(Input::get('depreciation_type'));
$assetdetail ->salvage_value = e(Input::get('salvage_value'));
$assetdetail ->asset_life = e(Input::get('asset_life'));




// Was the asset created?
if($assetdetail->save())
{
// Redirect to the saved location page
return Redirect::to("admin/settings/assetdetails/$assetdetailId/edit")->with('success', Lang::get('admin/assetdetails/message.update.success'));
}
}
else
{
// failure
$errors = $assetdetail->errors();
return Redirect::back()->withInput()->withErrors($errors);
}

// Redirect to the location management page
return Redirect::to("admin/settings/assetdetails/$assetdetailId/edit")->with('error', Lang::get('admin/assetdetails/message.update.error'));

}

我试过
{{ Form::checkbox('active', 1, true, Input::old('active', $assetdetail->active), array('class'=>'isnesdbox')) }}

但我得到了以下错误

不能将标量值用作数组

我也试过这个
<input class="col-md-1 controls isnesdbox" type="checkbox" name="active" checked id="active" value="1" {{ $assetdetail->active === '1' ? 'checked' : '' }} />

请帮助我实现这一目标
注意:iam 使用 mysql 数据库,iam 使用的数据库类型是 bit,它根据用户输入存储 0 和 1。

最佳答案

由于谷歌显然仍然认为这个答案是相关的,这里是 Laravel 5.8 的更新答案:

<input type="checkbox" value="true" name="notify" @if(!old() || old('notify') == 'true') checked="checked" @endif />

关于php - 如何在默认情况下选中复选框并从 Laravel 中的数据库中检索旧值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26530261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com