mardi 19 novembre 2019

Laravel save checkbox value in database

i would like to save the value (checked or unchecked) in my database, this is so when i refresh it shows the state it was left in when i press "save".

I was looking into this on this site and other, but just couldnt find the right thing. So i was looking for alternatives like LocalStorage, but this wasnt really what i was looking for.

Here's the code I'm trying to make work

<form action="" method="post">
                
                <div class="form-group">
                    <label for="followSwitch">Follow</label>
                    <div>
                    <label class="switch">
                        <input type="checkbox" name="follow" value="1" checked>
                        <span class="slider round"></span>
                    </label>
                    </div>

                </div>
                <div class="form-group">
                    <label for="likeSwitch">Like</label>
                    <div>
                    <label class="switch">
                        <input type="checkbox" name="like" value="1" checked>
                        <span class="slider round"></span>
                    </label>
                    </div>

                </div>
                <div class="form-group">
                    <label for="commentSwitch">Comment</label>
                    <div>
                    <label class="switch">
                        <input type="checkbox" name="comment" value="1" checked>
                        <span class="slider round"></span>
                    </label>
                    </div>
                </div>

            <div class="modal-footer">
                <a href="/instagram" class="btn btn-secondary">Close</a>
                <button type="submit" class="btn btn-primary">Save</button>
            </div>
        </form>

Part of the controller:

public function Config($id){
        $instagramAccount = InstagramAccount::find($id);
        return view('instagramAccountConfig', compact('instagramAccount', 'id'));
    }

Something i found regarding this (maybe unrelated?):

/* ROUTE FORCONFIG 
Route::post('/', function(){
    $box = Input::has('follow') ? true : false;
    $box = Input::has('like') ? true : false;
    $box = Input::has('comment') ? true : false;

    Config::create([
        'follow'=> $box,
        'like'=> $box,
        'comment'=> $box,
    ]);
    return 'created';
});
END*/

If anybody has any idea for this and my controller, (maybe more than just those 2) please leave a solution and/or a link to where I can find how to solve this.

Thanks.




Aucun commentaire:

Enregistrer un commentaire