簡體   English   中英

保存多個復選框-Laravel 5.4

[英]Save multiple checkbox - Laravel 5.4

我需要將多個復選框保存到數據庫中的單個字段。

            <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>

控制器:

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

這是嘗試保存[[“]中的值,我只需要保存數字

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

當我嘗試保存時,顯示下一條消息

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

您無法添加它,因為您可以嘗試以數組格式循環expresion_vegetal_id。 我在您的問題中看到您需要以字符串格式添加它。 您必須先循環該數組並保存一個數組,否則可以使用created

我用循環給你樣品。 您的代碼將如下所示

$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();
}

我希望您能找到其他相同的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM