You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中如何向多列插入多值?新手技术求助

解决Laravel多选框数据插入多列数据库的问题

Hey there! Let's work through this step by step. First, I spotted a small but crucial issue in your view that we need to fix first—your <option> tags don't have value attributes right now. Without them, when you submit the form, it'll send the visible text (like "English") instead of the code "En" you want stored in the database. Let's update that:

<select multiple="multiple" name="language[]" id="language" class="selectpicker" data-selected-text-format="count">
    <option value="En">English</option>
    <option value="It">Italian</option>
    <option value="Ar">Arabic</option>
    <option value="Jp">Japanese</option>
</select>

(Note: You don't need [] in the id attribute—only the name needs it to tell Laravel to treat the input as an array.)

Now, let's handle the insertion in your controller. The goal is to map each selected language code to its corresponding column in the languages table, leaving unselected columns empty. Here's how to do it:

Controller Code

Assuming you have an Eloquent model named Language (linked to your languages table), here's the store method:

use App\Models\Language;
use Illuminate\Http\Request;

public function store(Request $request)
{
    // Get selected languages, default to empty array if none are chosen
    $selectedLanguages = $request->input('language', []);

    // Initialize an array with all columns set to empty strings
    $languageData = [
        'English' => '',
        'Italian' => '',
        'Arabic' => '',
        'Japanese' => '',
    ];

    // Map each selected code to the correct database column
    foreach ($selectedLanguages as $code) {
        match ($code) {
            'En' => $languageData['English'] = 'En',
            'It' => $languageData['Italian'] = 'It',
            'Ar' => $languageData['Arabic'] = 'Ar',
            'Jp' => $languageData['Japanese'] = 'Jp',
        };
    }

    // Insert the data into the database
    Language::create($languageData);

    // Optional: Redirect back with a success message
    return redirect()->back()->with('success', 'Language preferences saved!');
}

Important Notes:

  • Mass Assignment: Make sure your Language model has the columns listed in the $fillable property to allow Laravel to insert data via create():
    // In app/Models/Language.php
    protected $fillable = ['English', 'Italian', 'Arabic', 'Japanese'];
    
  • Fallback for No Selection: Using $request->input('language', []) ensures we get an empty array instead of null if no languages are selected, which prevents errors in the loop.
  • Alternative with DB Facade: If you prefer not to use Eloquent, you can replace Language::create($languageData) with:
    DB::table('languages')->insert($languageData);
    

This will produce exactly the database output you're looking for—selected language codes in their respective columns, and empty values for unselected ones.

内容的提问来源于stack exchange,提问作者Sul Alo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:18:20