Reputation: 329
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 stores always "1", so the post is always active. Do you know where is the issue?
The dd($request->all()) shows:
"active" => "on"
Form radio buttons:
<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>
Then I have a PostStatusController with a update method and a edit method to show the view:
Update method:
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();
}
Edit method:
public function edit($id)
{
$post = Post::find($id);
return view('posts.edit')->with('post', $post);
}
Upvotes: 0
Views: 61
Reputation: 519
Your form should look like:
<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>
And update method should look like:
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();
}
Upvotes: 1
Reputation: 21681
Please update your code and try:
View file:
<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>
Update Method
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();
}
Upvotes: 1