插入多个数据Laravel 4

时间:2014-10-14 08:20:08

标签: php database laravel laravel-4

我试图循环一个表单并同时将多个输入插入到数据库中。循环表单运行正常但是当我尝试单击提交按钮时,它只会插入最后一个表单的输入。我做错了什么部分?

filename.blade.php

<div class="input-group input-group-sm">
      @for ($i = 0; $i < $gcount; $i++)
          <table>
            <tr>
              <td>{{ Form::label('title','*Title ',array('class'=>'class="control-label"')) }}</td>
              <td>{{ Form::select('title', ['Mr'=>'Mr', 'Ms'=>'Ms'],'Mr',array('class'=>'form-control')) }} </td>
            </tr>
            <tr>
              <td>{{ Form::label('birthday','*Birthday', array('class'=>'"input-group-addon btn"'))}}</td>
              <td>{{ Form::text('birthday', '', array('id' => 'calum','data-date-format'=>'yyyy-mm-dd','class' => 'form-control')) }}</td>
            </tr>
            <tr>
              <td>{{ Form::label('name','*Name ',array('class'=>'class="control-label"')) }} </td>
              <td>{{ Form::text('lastname', '',array('class' => 'form-control','placeholder'=>'lastname'))}}</td>
              <td>{{ Form::text('firstname', '',array('class' => 'form-control','placeholder'=>'firstname'))}}</td>
              <td>{{ Form::text('middlename', '',array('class' => 'form-control','placeholder'=>'middlename'))}}</td>
              <td></td>
            </tr>
         <hr>
      @endfor
            <tr>
              <td></td>
              <td></td>
              <td></td>
              <td><p class="text-right">{{ Form::submit('Submit',array('class'=>'btn btn-info btn-lg btn-block')) }}</td>
            </tr>
          </table>
  </div>

filenameController.php

$reserve = new Reserves;
foreach( $reserve as $key => $value) {
    $reserve->title = Input::get('title');
    $reserve->lastname = Input::get('lastname');
    $reserve->firstname= Input::get('firstname');
    $reserve->middlename = Input::get('middlename');
    $reserve->save(); 
}      

3 个答案:

答案 0 :(得分:3)

第一个问题是你的表格。您应该将所有表单字段的名称从title更改为title[] - 因此您应该将[]添加到所有所有表单字段(标题,姓氏,名字,中间名),也可能是生日(但你不在这里使用它)

现在你应该这样做:

$c = count(Input::get('title'));

$titles = Input::get('title');
$lastnames = Input::get('lastname');
$firstnames = Input::get('firstname');
$middlenames = Input::get('middlename');

for ( $i=0; $i< $c; ++$i) {
    $reserve = new Reserves;
    $reserve->title = $titles[$i];
    $reserve->lastname = $lastnames[$i];
    $reserve->firstname= $firstnames[$i];
    $reserve->middlename = $middlenames[$i];
    $reserve->save(); 
}   

答案 1 :(得分:1)

更改

$reserve = new Reserves;
foreach( $reserve as $key => $value) {
    $reserve->title = Input::get('title');
    $reserve->lastname = Input::get('lastname');
    $reserve->firstname= Input::get('firstname');
    $reserve->middlename = Input::get('middlename');
    $reserve->save(); 
}    

foreach( $reserve as $key => $value) {
    $reserve = new Reserves;
    $reserve->title = Input::get('title');
    $reserve->lastname = Input::get('lastname');
    $reserve->firstname= Input::get('firstname');
    $reserve->middlename = Input::get('middlename');
    $reserve->save(); 
}    

因为这将在循环中的每一轮创建不同的模型。您正在创建单个模型,然后多次更新。

答案 2 :(得分:0)

使用Eloquent::insert()

示例

$data =[];

foreach( $x as $y) {
    $data[] = [
        'title'      => $y->title,
        'lastname'   => $y->lastname,
        'firstname'  => $y->firstname,
        'middlename' => $y->middlename
    ];
}
Model::insert($data);