jeudi 26 novembre 2015

Dealing with checkboxes within controller

Looking to deal with checkboxes in a controller, Once an update is made.

However, I'm really struggling with the logic. I have in my DB table two columns titled : pack_compatible_android & pack_compatible_apple

If one checkbox is selected over the other, Both values in my DB still update as 1's.

I suspect my logic is just wrong and needs to either be simplified or can be simplified.

My code is as follows :

    // Save The Compatible
    $pack_compatible = Input::get('pack_compatible', false);

    if($pack_compatible == false)
    {
        $pack = Pack::find($id);
        $pack->pack_compatible_apple = NULL;
        $pack->pack_compatible_android = NULL;
        $pack->save();
    }
    else
    {
        if(is_array($pack_compatible))
        {
            foreach($pack_compatible as $compatible)
            {
                if($compatible == 'apple')
                {
                    $pack = Pack::find($id);
                    $pack->pack_compatible_apple = '1';
                    $pack->save();
                }
                elseif($compatible == 'android')
                {
                    $pack = Pack::find($id);
                    $pack->pack_compatible_android = '1';
                    $pack->save();
                }
            }
        }
    }

My blade code is as follows :

        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <div class="text-center">
                    <label>Compatible With <small>(For example, Apple)</small></label>
                </div><!-- /.text-center -->
                <div class="compatible-block">
                    <div class="row">
                        <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" value="apple" name="compatible[]" class="compat" <?php if($pack_data->pack_compatible_apple == '1') { echo 'checked'; } ?>>
                                    Apple
                                </label>
                            </div>
                        </div><!-- /.col-lg-6 -->
                        <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" value="android" name="compatible[]" class="compat" <?php if($pack_data->pack_compatible_android == '1') { echo 'checked'; } ?>>
                                    Android
                                </label>
                            </div><!-- /.checkbox -->
                        </div><!-- /.col-lg-6 -->
                    </div><!-- /.row -->
                </div><!-- /.compatible-block -->
            </div><!-- /.col-lg-12 -->
        </div><!-- /.row -->

Can anyone see any issues in my logic or as to why it doesn't seem to update one or the other?

Many Thanks




Aucun commentaire:

Enregistrer un commentaire