duannian7116 2018-03-28 16:46
浏览 49
已采纳

使用单选按钮更新无法正常工作

I have a posts table that has a tinyint column "active". If its "1" the post is active if "0" is inactive.

So I have a form where there are 2 radio buttons "active" and "inactive".

And if in db the column "active" is "1" the radio button "active" appears checked, if is "0" the radio button "inactive" appears checked. This is working fine.

But then to update is not working. When a radio button is selected and the "update" button is clicked it appears the "active" field as null. Do you know where is the issue?

array:2 [▼
  "active" => null
]

Form radio buttons:

<div class="form-group">
  <label for="active_inactive">Post Active or Inactive</label>
  <div class="hide-first">
    <div class="form-check">
      <input  {{  ($post->active) == 1 ? 'checked' : '' }}
      class="form-check-input radio" type="radio"
      name="active" value="1"
      id="{{$post->active}}">
      <label class="form-check-label" for="exampleRadios1">
        Active
      </label>
    </div>
    <div class="form-check">
      <input  {{  ($post->active) == 0 ? 'checked' : '' }}
      class="form-check-input radio" type="radio" name="active" id="{{$post->active}}" value="0">
      <label class="form-check-label" for="exampleRadios1">
        Inactive
      </label>
    </div>
  </div>
</div>

Update method:

public function update(Request $request, $id)
    {

        //dd($request->all());

        $post = Post::find($id);

        $post->active = $request->active ?: 0;

        $post->save();

        Session::flash('success', 'Post status updated.');

        return redirect()->back();
    }
  • 写回答

1条回答 默认 最新

  • doujian3401 2018-03-28 16:54
    关注

    You have to use larave dd() to see what you got in your request and you got 'on' from active field if you check it or if unchecked then you got null and if you wish then could use checkbox for simplify. This happened with you and you could use like this:

    $post->active = ($request->active == 'on') ? 1 : 0;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line