简体   繁体   English

Laravel:如何使用 select2 将多个数据插入 mysql 数据库?

[英]Laravel: How to insert multiple data with select2 to mysql database?

I am trying to insert data into database.我正在尝试将数据插入数据库。 But i don't know how to insert multi select data into the mysql database.但我不知道如何将多个 select 数据插入 mysql 数据库。

enter image description here在此处输入图像描述

<div class="form-group">
    <select name="languages[]" class="select2 form-control" multiple="multiple "required>
        <option selected>English</option>
        <option VALUE="french">French</option>
        <option VALUE="german">German</option>
        <option VALUE="spanish">Spanish</option>
        <option VALUE="turkish">Turkish</option>
        <option VALUE="italian">Italian</option>
    </select>
</div>
</div>

Controller Controller

public function store(Request $request)
{
    User::create($request->all());
    return redirect()->back();
}

return error返回错误

ErrorException Array to string conversion ErrorException 数组到字符串的转换

$request->languges is an array , and you can't save an array into a VARCHAR() field. $request->languges是一个数组,您不能将数组保存到VARCHAR()字段中。
In your name attribute languages[] will make an array , not string.以您的名字属性languages[]将创建一个数组,而不是字符串。 While name attribute languages make a string虽然名称属性languages制作一个字符串
If you have an array, then you need a foreach loop in your controller.如果您有一个数组,那么您的 controller 中需要一个 foreach 循环。
With select2 you need to make an array while sending multiple data.使用select2您需要在发送多个数据时创建一个数组
The table property needs to be a string not an array , So let's convert array to string : table属性需要是字符串而不是数组,所以让我们将数组转换为字符串

With Server-side使用服务器端

With foreach :使用foreach

public function store(Request $request)
{
    $data = $request->all();
    $x = NULL;
    foreach($request->languages as $key => $value) {
      $x .= $value .", ";
    }
    $data['languages'] = $x;
    User::create($data);
    return redirect()->back();
}

Or, with Implode :或者,使用内爆

public function store(Request $request)
{
    $data = $request->all();
    $data['languages'] = implode(',', $request->languages);
    User::create($data);
    return redirect()->back();
}

With Client-side, jQuery :使用客户端jQuery

Your title says "send selected values of select box to PHP as string".您的标题显示“将 select 框的选定值作为字符串发送到 PHP”。 You would do that in JS by catching the submit event of the form and using .join() to change the value of that field.您可以在 JS 中通过捕获表单的提交事件并使用.join()更改该字段的值来做到这一点。

$('#formid').on('submit', function(){
    $('#e1').val($('#e1').val().join(','));
});

Your form (not given) would need an id <form id="formid"...>您的表单(未给出)需要一个 id <form id="formid"...>

I'm not able to test this code, but here's the idea.我无法测试此代码,但这是我的想法。 In order to insert your data, you need to loop and insert it.为了插入您的数据,您需要循环并插入它。

foreach($request->languages as $data)
       {
           Languages::create($data);
       }
    return redirect()->back();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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