Laravel 5:以正确的方式将 implode() 与 Eloquent 结合使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43420396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5: Use implode() with Eloquent the right way
提问by Yousef Altaf
I have a checkbox
group and want to insert them in my database as val
(1,2,3).
我有一个checkbox
组,想将它们作为val
(1,2,3)插入到我的数据库中。
here is my blade
这是我的刀片
<div class="form-group">
@foreach($extra as $ext)
<div class="checkbox">
<label>
{{ Form::checkbox('extra_services[]', $ext->id, null, ['class' => 'checkbox']) }}
{!! $ext->title !!}
</label>
</div>
@endforeach
</div>
and here is controller
这是控制器
$temp->currency = $request->currency;
$temp->implode($request->extra_services, ',');
$temp->save();
I got
我有
strtolower() expects parameter 1 to be string, array given
strtolower() 期望参数 1 是字符串,给定数组
what is the right way to insert my checkbox values into my db as (1, 2, 3)?
将我的复选框值作为 (1, 2, 3) 插入到我的数据库中的正确方法是什么?
回答by Kris Roofe
You need change $temp->implode($request->extra_services, ',');
to $temp->extra_services = implode(',',$request->extra_services);
您需要更改$temp->implode($request->extra_services, ',');
为$temp->extra_services = implode(',',$request->extra_services);
回答by xar
I'm assuming you want to assign the value to $temp->extra_services
. The code then should be
我假设您想将值分配给$temp->extra_services
. 然后代码应该是
$temp->extra_services = collect($request->extra_services)->implode(',');