1

I know I can do the following to create multiple records with one query :

$users = [];
$users[] = ['name' => 'Tom'];
$users[] = ['name' => 'Jerry'];

$result = User::insert($users);

The problem of this approach is that ìnsert uses the query builder, and returns a boolean.

What if I wanted to have the created Eloquent models returned ? Would that be possible without having to do another query to retrieve them (how would I do that since I don't know the created ids ?) ?

I'm looking for something like the create method (which returns the created User model), but for multiple inserts (afaik createMany does not work outside of relationships)

Thanks

Nite
  • 323
  • 1
  • 4
  • 12
  • Hi @Nite please check following https://stackoverflow.com/questions/48472015/saving-multiple-records-in-a-laravel-eloquent-create – halilcakar Jun 30 '21 at 22:16
  • @halilcakar I already read that thread, and have nothing to do with my question unfortunately. – Nite Jun 30 '21 at 22:21
  • I'm sorry, I think I missed the part (afaik createMany..) while reading.. – halilcakar Jun 30 '21 at 23:23
  • 1
    So I was looking for this and saw this proposal on laravel issues which actually denied but this guy wrote a quick dirty way for it https://github.com/laravel/ideas/issues/1695#issuecomment-540745198 maybe something like this works? Or maybe you can tweek the code a bit – halilcakar Jun 30 '21 at 23:51

0 Answers0