I want to insert bulk on database and get rows id (first one, last one or all rows)
I tried insertGetId but it took only one row per time
Is there any way to get the query object of insert or run anonymous function with it to run $id = $query->getConnection()->getPdo()->lastInsertId($sequence); on it ?
Be careful with that. It will be the last id inserted into the db, and not necessarily from "this batch" if other queries are running at the same time or just before you retrieve the last id. This is not dependable unless you're absolutely sure no other queries are running during this time.
@MohammedAttya This is no more safe than what I said earlier, but it would get you the last inserted id by just selecting the highest id from the table.
$id = YourModel::latest('id')->value('id');
again, if any other insert queries were run on the table in between your bulk insert and when you check it, it won't be accurate.