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();
}
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;