简体   繁体   English

带有 postgresql db 的 Laravel 无法将值插入表并使用雄辩的方式获取最后插入的 id

[英]Laravel with postgresql db cant insert value to table and get last inserted id with eloquent

Laravel Framework 5.4.36 | Laravel 框架 5.4.36 | psql (PostgreSQL) 11.2 psql (PostgreSQL) 11.2

Hi, now I want to migrate my existing Laravel project DB from MySQL into PostgreSQL, I have some trouble here嗨,现在我想将我现有的 Laravel 项目数据库从 MySQL 迁移到 PostgreSQL,我在这里遇到了一些麻烦

$insert = [
   "a" => "name",
   "b" => age
   .
   .
];


return DB::table('table')->insert($insert);

when I try to insert DB with array data it returns like this,当我尝试插入带有数组数据的数据库时,它会像这样返回,

Undefined column: 7 ERROR:  column "0" of relation "table" does not exist ..
LINE 1: insert into "table" ("0", "1", "2", ..

it not return "a","b","c".. column but array position.它不返回 "a","b","c".. 列而是数组位置。 with MySQL DB it is working使用 MySQL DB 它正在工作


and

i try to get last inserted data id like this,我尝试像这样获取最后插入的数据 ID,

$data = [
   'name'=>$name,
   'address'=>$address,
   'id_class'=>$id_class,
   'lat'=>$lat,
   'long'=>$long,
   'is_changed'=>true
];
return Model::create($data)->id;

this will return undefine for id value这将返回 undefine 的 id 值


i update my code, 1st problem already resolved with this:我更新了我的代码,第一个问题已经解决了:

$insert[] = [
   "a" => "name",
   "b" => age
   .
   .
];

foreach(array_chunk($insert,5000) as $i){
  return DB::table('table')->insert($insert);
}

I once got the same error.我曾经遇到过同样的错误。 The issue was Accidentally i put问题是我不小心把

DB::beginTransaction();

In my auth middleware, forgot to close the beginTransaction by在我的 auth 中间件中,忘记通过以下方式关闭 beginTransaction

DB::commit();

that is why, every time i try to insert new value, i get the ID but entry doesn't appear in database.这就是为什么每次我尝试插入新值时,我都会得到 ID,但条目不会出现在数据库中。

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

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