Php 将元素数组存储到数据库(Laravel)
我需要关于如何将数组存储到数据库的建议。例如,我有一个带有Php 将元素数组存储到数据库(Laravel),php,laravel,laravel-5.2,Php,Laravel,Laravel 5.2,我需要关于如何将数组存储到数据库的建议。例如,我有一个带有name=“user\u phone[]”的输入,我想将该输入的值存储到数据库中。 我有一个这样的表单,也有其他输入,但我只复制了一个: {!! Form::open([route('some.router')]) !!} <fieldset class="form-group"> {{ Form::label(null, 'Phone') }} {{ Form::text('user_phone[]',
name=“user\u phone[]”
的输入,我想将该输入的值存储到数据库中。
我有一个这样的表单,也有其他输入,但我只复制了一个:
{!! Form::open([route('some.router')]) !!}
<fieldset class="form-group">
{{ Form::label(null, 'Phone') }}
{{ Form::text('user_phone[]', null, ['class' => 'form-control'] ) }}
</fieldset>
{!! Form::close() !!}
问题是,当我提交表单时出现错误:
htmlentities() expects parameter 1 to be string, array given
htmlentities() expects parameter 1 to be string, array given
我还使用强制转换将数组存储到DB:
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'user_phone' => 'array',
];
值存储正确,主要问题是validate()方法没有捕获错误。例如,我没有在需要的输入中填写一些数据。当我得到的不是像需要什么一样的错误,而是错误
htmlentities() expects parameter 1 to be string, array given
当我用数据填充所有输入时,一切正常。我认为问题来自于你的规则
'user_phone' => 'required
要验证数组值,应使用数组验证。()
像这样重写你的规则
"user_phone.0" => "required"
这将确保至少提供一部用户电话
如果您想验证电话格式,只需使用:
"user_phone.*" => "{Insert validation here}"
找到了定义
{!! Form::open([route('some.router')]) !!}
<fieldset class="form-group">
{{ Form::label(null, 'Phone') }}
{{ Form::text('user_phone[0]', null, ['class' => 'form-control'] ) }}
</fieldset>
{!! Form::close() !!}
和
validate()
方法捕获错误。这是我唯一的解决方案。您可以使用php的内爆
函数将逗号分隔的值存储到数据库中。{{Form::text('user_phone[]',null,['class'=>'Form control']])}
这样的输入是真的吗??用这个括号来存储值数组?或者在拉威尔还有别的办法?没问题。您是否确定错误是在验证时还是在订单创建时出现的?-也许有帮助。我真的不知道。我认为错误是在验证之前出现的。因为我在提交表单之后就出现了错误,所以我才意识到你选择用户的部分应该是这样的:$user=user::find($request->input('user_name'));我想知道保存部分是否正常(检查数据库中是否有新行),在呈现视图“admin.orders.index”时似乎会发生错误。检查如何使用视图中的“用户电话”字段
htmlentities() expects parameter 1 to be string, array given