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 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误