基于所选复选框的更新无法正常工作

时间:2018-03-25 18:18:00

标签: php laravel

我有一个表格帖子,其中有两列“share_inst”,“share_tw”为tinyints,默认情况下为“0”。

我有一个表单允许帖子的管理员选择我们是否希望在所有网站中共享帖子,或只是一个或没有。但它不起作用。

当我选择网站并点击“更新”时,我收到错误:

 SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'share_inst' cannot be null (SQL: update `posts` set `share_inst` = , `share_tw` =  where `id` = 1)

同样在表单中,如果在数据库中的列“share_inst”和“share_tw”和“1”,则不会显示复选框。

你知道错误在哪里吗?

表格

<form method="post" action="{{route('posts.share.update', ['post_id' => $post->id])"}}>
  {{csrf_field()}}
  {{ method_field('PATCH') }}

  <div class="form-group">
    <label for="inputName">
      Select which sites the post can be shared</label>
    <div class="form-check">
      <input class="form-check-input" type="checkbox" name="share_tw" id="share_tw" value="share_tw"
             {{ $post->share_tw == 0 ? 'checked' : '' }}>
      <label class="form-check-label" for="exampleRadios1">
        Twiiter
      </label>
    </div>
    <div class="form-check">
      <input class="form-check-input" type="checkbox" name="share_inst" id="share_inst" value="share_inst"
             {{ $post->share_inst == 1 ? 'checked' : '' }}>
      <label class="form-check-label" for="exampleRadios1">
        Instagram
      </label>
    </div>
  </div>
  <input type="submit" value="Update"/>
</form>

更新方法:

 public function update(Request $request, $id)
    {
        $post = Post::find($id);
        $post->share_tw = $request->share_tw;
        $post->share_ins = $request->share_ins;
        $post->save();
        return redirect()->back();
    }

修改方法

 public function edit($id)
    {
        $post = Post::find($id);

        return view('posts.share.edit')->with('post', $post);
    }

路线:

// update shared
Route::get('post/edit/{id}/share',    [ 'uses' => 'ShareController@edit', 'as'=>'posts.share.edit']);
Route::post('post/update/{id}/share', [ 'uses' => 'ShareController@update', 'as'=>'posts.share.update']);

3 个答案:

答案 0 :(得分:0)

未选中复选框未提交,您需要将更新代码更改为:

    $post->share_tw = !is_null($request->share_tw);
    $post->share_ins = !is_null($request->share_ins);

这会将选中的复选框转换为布尔值,以确保您的查询正确执行。

答案 1 :(得分:0)

未提交未选中复选框,因此您需要检查输入是否存在

removeFirst

答案 2 :(得分:0)

As others have pointed out, if a checkbox is not ticked, it is not usually included in the request. When a checkbox is ticked, its value is included in the request.

Try updating your update method to something like this:

public function update(Request $request, $id)
{
    $post = Post::find($id);
    $post->share_tw = $request->input('share_tw', 0);
    $post->share_ins = $request->input('share_ins', 0);
    $post->save();
    return redirect()->back();
}

And set the value of each of your checkboxes to be 1 in your view:

<input class="form-check-input" type="checkbox" name="share_tw" id="share_tw" value="1"{!! $post->share_tw == 1 ? ' checked' : '' !!}>
<input class="form-check-input" type="checkbox" name="share_inst" id="share_inst" value="1"{!! $post->share_inst == 1 ? ' checked' : '' !!}>

So what happens here is that when you submit the form, you're actually submitting the value 1 against each of your two checkboxes, if they're ticked. If they're not ticked, nothing is passed through in the request, but by using the input method and specifying a second parameter as a default value, you're saying that if there's no value for your checkbox, use 0. This way you'll either end up setting the value to a 1 or a 0.

If you'd rather not use the input method, and you would prefer to access the value from the request as if it were a property, you could do this instead to get the same result:

$post->share_tw = $request->share_tw ?: 0;
$post->share_ins = $request->share_ins ?: 0;