mercredi 3 janvier 2018

Laravel/php - creating an array of boolean values from multiple checkboxes

I"m attempting to create an array of a user's selections from 4 checkboxes, where the values are boolean (1 or 0). Here's where I'm at so far:

$selections = [
        'marketing' => $request->get('marketing'),
        'promotional' => $request->get('promotional'),
        'news' => $request->get('news'),
        'feedback' => $request->get('feedback')
    ];

    foreach ($selections as $selection) {
        if ($selection === null) {
            return $selection = 0);
        }
    }
    dd($selections);

Trying to foreach through and check if the checkbox was deselected(null) and set that to zero. I don't think I can do this in my form, but here's that code as well just in case:

<div class="form-group col-sm-12">
        <label>Uncheck the categories you would like to not receive emails from.</label>
        <div class="checkbox">
            <br/>
            <label>
            
            Marketing</label>
        </div>
        <div class="checkbox">
            <br/>
            <label>
            
            Promotional</label>
        </div>
        <div class="checkbox">
            <br/>
            <label>
            
            News</label>
        </div>
        <div class="checkbox">
            <br/>
            <label>
            
            Feedback</label>
        </div>
    </div>




Aucun commentaire:

Enregistrer un commentaire