Radio button always take boolean 0 in laravel

2019-04-15 15:05发布

问题:

I have created radio button fields as follows

EDIT: I have changed form as suggested in comments

<div class="form-group">
    {!! Form::label('is_kids_friendly','Kids Friendly:') !!}
    {!! Form::radio('kidsfriendly', 'true', null),'&nbsp', 'Yes' !!}
    {!! Form::radio('kidsfriendly', 'false', null),'&nbsp','No' !!}
</div>

<div class="form-group">
    {!! Form::label('is_kids_only','Kids Only:') !!}
    {!! Form::radio('kidsonly', 'true', null),'&nbsp', 'Yes' !!}
    {!! Form::radio('kidsonly', 'false', null),'&nbsp','No' !!}
</div>

<div class="form-group">
    {!! Form::label('sineor_citizan_friendly','Sineor Citizan Friendly:') !!}
    {!! Form::radio('seniorcitizen', 'true', null),'&nbsp', 'Yes' !!}
    {!! Form::radio('seniorcitizen', 'false', null),'&nbsp','No' !!}
</div>

As this is radio button field, i have used boolean in database. Each time, i fill in details, It takes 0. Also, i can select multiple radio button, thats should not happen. Am i doing something wrong?

回答1:

You should do following changes then your controller query will work as you want!

    <div class="form-group">
    {!! Form::label('is_kids_friendly','Kids Friendly:') !!}
    {!! Form::radio('is_kids_friendly', true, null),'&nbsp', 'Yes' !!}
    {!! Form::radio('is_kids_friendly', false, null),'&nbsp','No' !!}
</div>

<div class="form-group">
    {!! Form::label('is_kids_only','Kids Only:') !!}
    {!! Form::radio('is_kids_only', true, null),'&nbsp', 'Yes' !!}
    {!! Form::radio('is_kids_only', false, null),'&nbsp','No' !!}
</div>

<div class="form-group">
    {!! Form::label('sineor_citizan_friendly','Sineor Citizan Friendly:') !!}
    {!! Form::radio('sineor_citizan_friendly', true, null),'&nbsp', 'Yes' !!}
    {!! Form::radio('sineor_citizan_friendly', false, null),'&nbsp','No' !!}
</div>


回答2:

The name should be same with different values. Try with -

{!! Form::label('is_kids_friendly','Kids Friendly:') !!}
{!! Form::radio('is_kids_friendly', 'yes', true),'&nbsp', 'Yes' !!}
{!! Form::radio('is_kids_friendly', 'no'),'&nbsp','No' !!}

The arguments are - radio('name of field', 'value', 'checked or not')



回答3:

{!! Form::label('is_kids_friendly','Kids Friendly:') !!}
{!! Form::radio('value', 'yes', null),'&nbsp', 'Yes' !!}
{!! Form::radio('value', 'no', null),'&nbsp','No' !!}

Try this: first parameter is name of radio button , second parameter is value, third is default checked



回答4:

You are passing $request->all() directly to the create method, in which case your HTML field's name should be same as the column names in your DB

<div class="form-group">
 {!! Form::label('is_kids_friendly','Kids Friendly:') !!}
 {!! Form::radio('is_kids_friendly', 'true', null),'&nbsp', 'Yes' !!}
 {!! Form::radio('is_kids_friendly', 'false', null),'&nbsp','No' !!}
</div>

<div class="form-group">
 {!! Form::label('is_kids_only','Kids Only:') !!}
 {!! Form::radio('is_kids_only', 'true', null),'&nbsp', 'Yes' !!}
 {!! Form::radio('is_kids_only', 'false', null),'&nbsp','No' !!}
</div>

<div class="form-group">
 {!! Form::label('senior_citizen_friendly','Sineor Citizan Friendly:') !!}
 {!! Form::radio('senior_citizen_friendly', 'true', null),'&nbsp', 'Yes' !!}
 {!! Form::radio('senior_citizen_friendly', 'false', null),'&nbsp','No' !!}
</div>