dqqs64238 2015-06-28 16:22
浏览 33
已采纳

Laravel CSV使用相同ID导入插入(简单导入)

This is my store method which grabs the file and loops through each of the rows:

public function store()
    {
        $input = Input::file('statuses');
        $filename = $input->getRealPath();
        $i = 0;

        $rows = Excel::load($filename, null, 'ISO-8859-1')->get()->toArray();

        foreach($rows as $k => $row)
        {
            if(!isset($err)) {
                if (!$this->repository->create($row))
                    $err = 'Error importing row ' + $i;
                    $i++;
                }
        }

        if(isset($err)) {
            Flash::error($err);
            return Redirect::route('admin.importstatus.index');
        }

        Flash::success('Statuses Imported!');
        return Redirect::route('admin.statuses.index');
    }

In my repository, my create method looks like this:

public function create(array $data)
{
    // Create the model
    $model = $this->model->fill($data);

    if ($model->save()) {
        return $model;
    }

    return false;
}

Now, what appears to be happening when I import 6 rows only the final is actually getting inserted into the DB.

If I var_dump in my create method, I am being returned the following:

    array (size=7)
  'content' => string 'Imported two' (length=12)
  'status' => float 0
  'user_id' => float 1
  'pinned' => float 0
  'updated_at' => string '2015-06-28 16:13:22' (length=19)
  'created_at' => string '2015-06-28 16:13:22' (length=19)
  'id' => int 8
array (size=7)
  'content' => string 'Imported three' (length=14)
  'status' => float 0
  'user_id' => float 1
  'pinned' => float 0
  'updated_at' => string '2015-06-28 16:13:22' (length=19)
  'created_at' => string '2015-06-28 16:13:22' (length=19)
  'id' => int 8
array (size=7)
  'content' => string 'Imported four' (length=13)
  'status' => float 0
  'user_id' => float 1
  'pinned' => float 0
  'updated_at' => string '2015-06-28 16:13:22' (length=19)
  'created_at' => string '2015-06-28 16:13:22' (length=19)
  'id' => int 8

Notice how each of the ID's are all no. 8 (The next available row in the table). The tables ID is defo AUTO INCREMENT etc so no issues there, I guess its a logic issue? Any ideas?

  • 写回答

1条回答 默认 最新

  • dsdsds12222 2015-06-28 16:25
    关注

    Seems like you are using the same instance of a model over and over.

    Try changing fill():

    $this->model->fill($data);
    

    with create():

    $this->model->create($data);
    

    With fill() you are only filling the already created model instance with some data. But if you use create() you are first creating a new instance (with a new id), then filling it with data and then saving it.

    Important

    When using create() you are also persisting it to the database, which means you don't have to save() it manually.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大