Eloquent - 将数据数组插入表中

时间:2018-04-26 04:42:00

标签: laravel eloquent

Heei,我只是使用 邀请人 创建一些 预订房间 网络。该表看起来像那样

enter image description here

enter image description here

关于 预订房间 的数据将被插入jadwal_meetings表格以及有关 邀请人 的数据将插入invites表。控制器如下所示

控制器

public function save_schedule(Request $save)
    {
      $create = new jadwal_meeting;
      $create->nama_meeting = $save->input('nama_meeting');
      $create->tanggal = $save->input('tanggal_meeting');
      $create->jam_mulai = $save->input('jam_mulai');
      /*and some other field*/

      $id_jadwal = $create->id;
      $create->save();
      // looping email_user
      $emails = $save->input('email');
      foreach ($emails as $email)
      {
        // Insert to invites table
        $email = new invites;
        $email->id_meeting = $save->$id_jadwal;
        $email->invite_people = $save->input('email');
        $email->save();
      }
      return redirect()->route('user_home');
    }

我只是创建了html 阴影输入元素,用于收集 邀请人 这样的值

Html + Jquery

<input type="text" name="nama_meeting">
<input type="text" name="tanggal_meeting">
<input type="email" name="people_invited">
<ul class="people_added"></ul>

$("[name='people_invited']").keyup(function (e) {
      if (e.key == 'Enter' && ($(this).val() !== '')) {
        var orang_yg_diinvite = $(this).val();
        $("ul.people_added").append("<li>" + orang_yg_diinvite +"</li>");
        $("ul.people_added").append("<input type='hidden' name='email[]' value='" + orang_yg_diinvite + "'>");
      }
    });

结果

enter image description here

但这不行,我得到这样的错误

  

数组到字符串转换(SQL:插入invitesid_meeting,   invite_peopleupdated_atcreated_at)值(,   people1 @email.com,2018-04-26 04:37:50,2018-04-26 04:37:50))

如何解决这个错误,请帮帮我:)。

2 个答案:

答案 0 :(得分:4)

您只需更改控制器代码,如下所示。

public function save_schedule(Request $save)
    {
      $create = new jadwal_meeting;
      $create->nama_meeting = $save->input('nama_meeting');
      $create->tanggal = $save->input('tanggal_meeting');
      $create->jam_mulai = $save->input('jam_mulai');
      /*and some other field*/

      $id_jadwal = $create->id;
      $create->save();
      // looping email_user
      $emails = $save->input('email');
      foreach ($emails as $email)
      {
        // Insert to invites table
        $invite = new invites;
        $invite->id_meeting = $id_jadwal; // changes
        $invite->invite_people = $email; // changes
        $invite->save();
      }
      return redirect()->route('user_home');
    }

谢谢,

答案 1 :(得分:2)

你犯了一些错误。请尝试此代码。

$create = new jadwal_meeting;
$create->nama_meeting = $save->input('nama_meeting');
$create->tanggal = $save->input('tanggal_meeting');
$create->jam_mulai = $save->input('jam_mulai');
$create->save();
$id_jadwal = $create->id;

$emails = $save->input('email');
foreach ($emails as $email)
{
    // Insert to invites table
    $invite = new invites;
    $invite->id_meeting = $id_jadwal;
    $invite->invite_people = $email;
    $invite->save();
}