繁体   English   中英

Laravel 5.0:如何在同一列数据库中保存多行输入?

[英]Laravel 5.0: How to save multiple inputs of rows in the same column of database?

我是Laravel的绝对初学者,处理某个问题。 英语不是我的第一语言,所以如果这篇文章对你没有意义或者你需要更多信息,请发表评论。

我想将多个数据输入插入到数据库表的单个列中。

我做了一个带有桌子的页面。 例如,教师将在“讲师评论”栏的第一行和第二行留下评论。

但是,我很难找到一种方法。 我正在处理下面的错误。

任何意见,将不胜感激。 提前致谢!

create.blade.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() :参数不匹配,pattern是一个字符串,而replacement是一个数组

在您的模型中,您可以添加一些可以帮助的变更器:

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模型中。

暂无
暂无

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

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