簡體   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