更新无法正常工作

时间:2018-03-29 12:59:55

标签: php laravel

我有一个帖子表,其中有一个tinyint列"活跃"。如果它的" 1"如果" 0"没有用。

所以我有一个表格,其中有2个单选按钮"活动"和"不活跃"。

如果在db中列#34;活跃"是" 1"单选按钮"活动"出现检查,如果是" 0"单选按钮"不活动"出现检查。这很好。

但是更新不起作用。当选择一个单选按钮并且"更新"单击按钮,它始终存储" 1",因此帖子始终处于活动状态。你知道问题出在哪里吗?

dd($ request-> all())显示:

 "active" => "on"

表单单选按钮:

<form method="post" class="clearfix" action="{{route('posts.update', ['post_id' => $post->id])}}">
    {{csrf_field()}}
    <div class="form-group">
        <label>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"
                        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}}">
                <label class="form-check-label" for="exampleRadios1">
                    Inactive
                </label>
            </div>
        </div>
    </div>
    <input type="submit" value="Update"/>
</form>

然后我有一个PostStatusController,它有一个更新方法和一个编辑方法来显示视图:

更新方法:

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

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

        $post = Post::find($id);
        $post->active = ($request->active == 'on') ? 1 : 0;
        $post->save();

        Session::flash('success', 'Post status updated.');
        return redirect()->back();
    }

修改方法:

public function edit($id)
{
    $post = Post::find($id);
    return view('posts.edit')->with('post', $post);
}

2 个答案:

答案 0 :(得分:1)

请更新您的代码并尝试:

查看文件:

<form method="post" class="clearfix" action="{{route('posts.update', ['post_id' => $post->id])}}">
    {{csrf_field()}}
    <div class="form-group">
        <label>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"
                        id="{{$post->active}}" value="1">
                <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>
    <input type="submit" value="Update"/>
</form>

更新方法

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

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

        $post = Post::find($id);
        $post->active = $request->active;
        $post->save();

        Session::flash('success', 'Post status updated.');
        return redirect()->back();
    }

答案 1 :(得分:1)

您的表单应如下所示:

<form method="post" class="clearfix" action="{{route('posts.update', ['post_id' => $post->id])}}">
    {{csrf_field()}}
    <div class="form-group">
        <label>Active or Inactive</label>
        <div class="hide-first">
            <div class="form-check">
                <input  {{  old('active', $post->active) ? 'checked' : '' }}
                        class="form-check-input radio"
                        type="radio"
                        name="active"
                        id="active"
                        value="1">
                <label class="form-check-label" for="exampleRadios1">
                    Active
                </label>
            </div>
            <div class="form-check">
                <input  {{  old('active', $post->active) ? '' : 'checked' }}
                        class="form-check-input radio"
                        type="radio"
                        name="active"
                        id="inactive"
                        value="0">
                <label class="form-check-label" for="exampleRadios1">
                    Inactive
                </label>
            </div>
        </div>
    </div>
    <input type="submit" value="Update"/>
</form>

更新方法应如下所示:

public function update(Request $request, $id)
{
    $post = Post::find($id);
    $post->active = !!$request->active;
    $post->save();

    Session::flash('success', 'Post status updated.');
    return redirect()->back();
}