如何在一列中存储多个值 [英] How to store multiple values in one column
问题描述
我想在laravel项目中存储来自select2
多个值的多个值.它返回一个数组,其中所有输入都包含服务ID.我想将每个值存储到service_id
列中的数据库中.我怎样才能做到这一点?我是使用laravel 5.8刚接触laravel的人.
dd()
请求的输出:
这是我的代码:
$services = array();
$services = $request->except('_token');
foreach ($services as $id=>$value) {
DB::table('services')->insert(['service_id' => $value]);
}
它给出此错误:找不到列:1054'字段列表'中的未知列'0'(SQL:插入services
(0
,1
)值(6,9))" /p>
我该如何解决?预先感谢.
问题是您正在将数组传递给服务ID. 在您的情况下,键将是services_select,而value是您的foreach循环中的一个数组.
尝试一下
$services = $request->except('_token');
foreach ($services['services_selects'] as $id => $value) {
DB::table('services')->insert(['service_id' => $value]);
}
希望这会有所帮助.
I want to store multiple values which comes from select2
multiple values in my laravel project. It returns an array with all inputs contains service ids. I want to store each value into database in the service_id
column. How can I do this? I am new to laravel, using laravel 5.8.
dd()
output of request:
here is my code:
$services = array();
$services = $request->except('_token');
foreach ($services as $id=>$value) {
DB::table('services')->insert(['service_id' => $value]);
}
It gives this error: "Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into services
(0
, 1
) values (6, 9))"
how can I solve this? thanks in advance.
The problem is you are passing an array to service id. In your case the key will be services_select and value is an array in your foreach loop.
Try this
$services = $request->except('_token');
foreach ($services['services_selects'] as $id => $value) {
DB::table('services')->insert(['service_id' => $value]);
}
Hope this will help.
这篇关于如何在一列中存储多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!