Laravel-如何更新复选框字段值

时间:2019-12-30 06:00:02

标签: laravel

使用Laravel-5.8,我已经能够将数据保存到数据库中,包括复选框字段。

protected $fillable = [
    'appraisal_name',
    'is_current',
    'appraisal_start',
    'appraisal_end',
];
public function rules()
{
    return [
        'appraisal_name'  => 'required|min:5|max:100',
        'appraisal_start' => 'required',
        'appraisal_end'   => 'required|after_or_equal:appraisal_start',
        'is_current'      => 'nullable|boolean',
    ];
}
public function create()
{
    return view('appraisal.appraisal_identities.create');
}

public function store(StoreAppraisalIdentityRequest $request)
{
    $identity = AppraisalIdentity::create([
        'appraisal_name'  => $request->appraisal_name,
        'appraisal_start' => $appraisalStart,
        'appraisal_end'   => $appraisalEnd,
        'is_current'      => $request->has('is_current'),
    ]);
    Session::flash('success', 'Appraisal Initialization is created successfully');

    return redirect()->route('appraisal.appraisal_identities.index');
}

public function edit($id)
{
    abort_unless(\Gate::allows('appraisal_identity_edit'), 403);
    $identity = AppraisalIdentity::where('id', $id)->first();

    return view('appraisal.appraisal_identities.edit')->with('identity', $identity);
}

public function update(UpdateAppraisalIdentityRequest $request, $id)
{
    abort_unless(\Gate::allows('appraisal_identity_edit'), 403);

    $appraisalStart = Carbon::parse($request->appraisal_start);
    $appraisalEnd = Carbon::parse($request->appraisal_end);
    $submissionStart = Carbon::parse($request->submission_start);
    $submissionEnd = Carbon::parse($request->submission_end);

    $identity = AppraisalIdentity::find($id);
    $identity->appraisal_name = $request->appraisal_name;
    $identity->appraisal_start = $appraisalStart;
    $identity->appraisal_end = $appraisalEnd;
    $identity->is_current = $request->has('is_current');
    $identity->save();
    Session::flash('success', 'Appraisal Initialization is updated successfully');

    return redirect()->route('appraisal.appraisal_identities.index');
}

create.blade

<form  action="{{route('appraisal.appraisal_identities.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
    {{csrf_field()}}
    <div class="form-body">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group row">
                     <label class="control-label text-right col-md-3">Is Current Appraisal?</label>
                      <div class="col-md-9">
                          <input type="checkbox" class="form-control"  name="is_current" value="{{old('is_current')}}">
                      </div>
                  </div>
              </div>
          </div>
      </div>
      <div>
          <!--<input class="btn btn-primary" type="submit" value="{{ trans('global.save') }}">-->
          &nbsp;&nbsp;&nbsp;<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
          <button type="button" onclick="window.location.href='{{route('appraisal.appraisal_identities.index')}}'" class="btn btn-default">Cancel</button>
      </div>
</form>

edit.blade

 <form  action="{{route('appraisal.appraisal_identities.update', ['id'=>$identity->id])}}" method="post" class="form-horizontal" enctype="multipart/form-data">

    {{ csrf_field() }}
    <input name="_method" type="hidden" value="PUT">
    <div class="form-body">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group row">
                     <label class="control-label text-right col-md-3">Is Current Appraisal?</label>
                      <div class="col-md-9">
                          <input type="checkbox" class="form-control"  name="is_current" value="{{old('is_current')}}">
                      </div>
                  </div>
              </div>
          </div>
      </div>
      <div>
          <!--<input class="btn btn-primary" type="submit" value="{{ trans('global.save') }}">-->
          &nbsp;&nbsp;&nbsp;<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
          <button type="button" onclick="window.location.href='{{route('appraisal.appraisal_identities.index')}}'" class="btn btn-default">Cancel</button>
      </div>
</form>

尝试更新数据时遇到这些问题?

  1. 在加载时,编辑复选框字段(is_current)没有从数据库中选择值。仍未选中。

  2. is_current为0或1。目标是,表中只能有一个is_current字段设置为1。从复选框中,将is_current选中为1时,应将表中其他任何is_current设置为1到0。

我该如何解决这些问题?

谢谢。

2 个答案:

答案 0 :(得分:0)

在编辑刀片中,如下更新您的复选框代码:

<input type="checkbox" class="form-control"  name="is_current" @if($identity->is_current == 1) checked @endif value="{{old('is_current')}}">

在您的更新功能中更改以下内容:

public function update(UpdateAppraisalIdentityRequest $request, $id)
{
    abort_unless(\Gate::allows('appraisal_identity_edit'), 403);

    $appraisalStart = Carbon::parse($request->appraisal_start);
    $appraisalEnd = Carbon::parse($request->appraisal_end);
    $submissionStart = Carbon::parse($request->submission_start);
    $submissionEnd = Carbon::parse($request->submission_end);

    $identity = AppraisalIdentity::find($id);
    $identity->appraisal_name = $request->appraisal_name;
    $identity->appraisal_start = $appraisalStart;
    $identity->appraisal_end = $appraisalEnd;
    $identity->is_current = $request->has('is_current');
    $identity->save();

    // this line update all column to 0 and leave $id field
    AppraisalIdentity::where('id', '!=', $id)->update(['is_current' => 0]);

    Session::flash('success', 'Appraisal Initialization is updated successfully');

    return redirect()->route('appraisal.appraisal_identities.index');
}

在商店功能中更改以下内容:

public function store(StoreAppraisalIdentityRequest $request)
{
    $identity = AppraisalIdentity::create([
        'appraisal_name'  => $request->appraisal_name,
        'appraisal_start' => $appraisalStart,
        'appraisal_end'   => $appraisalEnd,
        'is_current'      => $request->has('is_current'),
    ]);


    $id = $identity->id;
    // this line update all column to 0 and leave $id field
    AppraisalIdentity::where('id', '!=', $id)->update(['is_current' => 0]);

    Session::flash('success', 'Appraisal Initialization is created successfully');

    return redirect()->route('appraisal.appraisal_identities.index');
}

答案 1 :(得分:0)

在编辑刀片中尝试此操作 < is_current == 1吗?已检查:value =“ {{old('is_current')}}”>“> p