Laravel 5.0:如何在数据库的同一列中保存行的多个输入
Laravel 5.0: How to save multiple inputs of rows in the same column of database?
我是Laravel和处理某个问题的绝对初学者。英语不是我的第一语言,所以如果这篇文章对你没有意义,或者你需要更多信息,请留下评论。
我想将多个数据输入插入到数据库表的单列中。
我做了一个页面,里面有一个表格。例如,讲师会在"讲师评论"列的第一行和第二行都留下评论。
然而,我很难找到这样做的方法。我正在处理下面的错误。
如有任何建议,我们将不胜感激。提前感谢!
createblade.php
{!! Form::open(['url' => 'logs']) !!}
<tbody>
<tr>
<td class="weeks">
{!! Form::selectRange('weeks[]', 1, 17) !!}
</td>
<td class="work_description">
{!! Form::textarea('work_description[]', null) !!}
</td>
<td class="instructor_comments">
{!! Form::textarea('instructor_comment[]', null) !!}
</td>
<td class="status">
{!! Form::text('status[]', null) !!}
</td>
</tr>
<tr>
<td class="weeks">
{!! Form::selectRange('weeks[]', 1, 17) !!}
</td>
<td class="work_description">
{!! Form::textarea('work_description[]', null) !!}
</td>
<td class="instructor_comments">
{!! Form::textarea('instructor_comment[]', null) !!}
</td>
<td class="status">
{!! Form::text('status[]', null) !!}
</td>
</tr>
</tbody>
create_logs_table.php
public function up()
{
Schema::create('logs', function(Blueprint $table)
{
$table->increments('id');
$table->integer('weeks');
$table->text('work_description');
$table->text('instructor_comments');
$table->string('status');
$table->timestamps();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
错误::
preg_replace()
:参数不匹配,模式是字符串,而替换是数组
在您的模型中,您可以添加一些有帮助的突变体:
public function setInstructorCommentsAttribute( $value )
{
$this->attributes['instructor_comments'] = json_encode( $value );
}
public function getInstructorCommentsAttribute()
{
$type = json_decode( $this->attributes['instructor_comments'] );
return $type;
}
setInstructorCommentsAttribute
将Input数组转换为JSON对象,并将其保存到数据库中。然后getInstructorCommentsAttribute
将JSON对象解码回一个数组,供您在代码中使用。
将此代码添加到您的logs
模型中。
相关文章:
- 柱状图后面的一列
- 数据表通过分析一列的值对其进行排序
- 一次保存多个主干模型
- Datatables-如何在未显示所有列的情况下禁用表中最后一列的排序
- 获取表中每行的一列的内容,然后循环访问每个内容
- 对象数组-与其他列数据相比,增加一列
- 在最初的查询/表格绘制中只提取谷歌电子表格的一列
- jQuery Datatables:单击一列时对另一列进行排序
- 我的砖石图像只显示在一列中
- 删除“;使用javascript从pentaho中的一列中选择“[”
- 使用 Javascript 在一列中更改属性内容
- jquery插件 - 已选择 - 多项选择不在一行中列出,而是列在一列中
- 单击包含按钮的最后一列时获取表的第一列值
- 排除行选择中的最后一列
- 取消表中一列的onclick事件
- “搜索”文本仅考虑表格中的最后一列
- jqGrid有条件地创建一列
- 当第一次砌筑'项目'将所有剩余项隐藏在一列中
- 避免在rich:dataTable的某一列中出现onRowClick事件
- 如何在jQuery数组中保存行的每一列的内容