简体   繁体   中英

Save multiple checkbox - Laravel 5.4

I need to save multiple checkbox to single field in the database.

            <div class="checkbox">
                <label>
                    <input type="checkbox" name="expresion_vegetal_id[]" value="1">Raíz
                </label>
            </div>

            <div class="checkbox">
                <label>
                    <input type="checkbox" name="expresion_vegetal_id[]" value="3">tronco
                </label>
            </div>
            <div class="checkbox">
                <label>
                    <input type="checkbox" name="expresion_vegetal_id[]" value="4">corteza
                </label>
            </div>

Controller:

$ficha_tecnica = new Ficha_Tecnica();
$options = $request->get('expresion_vegetal_id');
$ficha_tecnica->expresion_vegetal_id = $options;
$ficha_tecnica->save();

this is trying to save, the values ​​in [""], I need only save the numbers

insert into `fichas_tecnicas` (`expresion_vegetal_id`) values (["1","3","4"])

When I try to save, show the next message

1366 Incorrect integer value: '["1","4"]' for column 'expresion_vegetal_id' 

You'r cannot add that because You can try to loop the expresion_vegetal_id still in array format. I see in your question is you need to add that in string format. You must loop that array first and save one by one or you can used created

I give you the sample using loop. You code will looks like this

$ficha_tecnica = new Ficha_Tecnica();
foreach ($$request->expresion_vegetal_id as $expresion_vegetal_id) {
    $ficha_tecnica->fresh();
    $ficha_tecnica->expresion_vegetal_id = $expresion_vegetal_id;
    $ficha_tecnica->save();
}

i hope you can find the other same way....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM