在 Laravel 内批量创建模型的最佳方式?
Best way to bulk create models within Laravel?
我正在创建一项功能,其中涉及创建折扣券代码。我包含一个输入,用户可以填写他们想要的具体数量。
当它点击存储方法时,如果用户输入了 15 张凭证,您将如何一次性创建那么多模型记录?
通过查看 laravel 文档,我唯一看到的是使用工厂,所以我在下面尝试了这个。
public function storeCodes(Request $request, VoucherGroup $voucherGroup)
{
VoucherCode::create([
'code' => $this->generateUniqueCode(),
'group_id' => $voucherGroup->id,
'used' => 0
]);
session()->flash('success', 'successfully created codes.');
return back();
}
private function generateUniqueCode()
{
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersNumber = strlen($characters);
$code = '';
while (strlen($code) < 6) {
$position = rand(0, $charactersNumber - 1);
$character = $characters[$position];
$code = $code . $character;
}
if (VoucherCode::where('code', $code)->exists()) {
dd('lol');
$this->generateUniqueCode();
}
return $code;
}
输入
<x-inputs.input size="6" type="number" name="voucher_amount" required>How many Voucher codes do you want
to
generate?
</x-inputs.input>
问题是它创建了模型,但代码 returns 每个模型的代码相同。
如何在一次调用中创建 15 个具有唯一代码字段的模型?
下面的 for 循环为我解决了这个问题。它最初是由某人发布的,但最近被删除了,所以我无法回答下面的问题,但感谢提供它的人!
$newVouchers = [];
for ($i = 0; $i < $request->voucher_amount; $i++) {
$newVouchers[] = VoucherCode::create([
'code' => $this->generateUniqueCode(),
'group_id' => $voucherGroup->id,
'used' => 0
]);
}
您可以稍后试用
do {
//generate unique code
$uniqueCode = strtoupper(substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 15));
} while (VoucherCode::where('code', $uniqueCode)->exists());
VoucherCode::create([
'code' => $uniqueCode,
'group_id' => $voucherGroup->id,
'used' => 0
]);
session()->flash('success', 'successfully created codes.');
return back();
您可以使用 Insert Statements 批量创建模型
$代金券=[];
对于 ($i = 0; $i < $numberOfVoucher; $i++) {
$代金券[] = [
'code' => $this->generateUniqueCode(),
'group_id' => $voucherGroup->id,
'used' => 0
];
}
DB::table('users')->插入($vouchers);
我正在创建一项功能,其中涉及创建折扣券代码。我包含一个输入,用户可以填写他们想要的具体数量。
当它点击存储方法时,如果用户输入了 15 张凭证,您将如何一次性创建那么多模型记录?
通过查看 laravel 文档,我唯一看到的是使用工厂,所以我在下面尝试了这个。
public function storeCodes(Request $request, VoucherGroup $voucherGroup)
{
VoucherCode::create([
'code' => $this->generateUniqueCode(),
'group_id' => $voucherGroup->id,
'used' => 0
]);
session()->flash('success', 'successfully created codes.');
return back();
}
private function generateUniqueCode()
{
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersNumber = strlen($characters);
$code = '';
while (strlen($code) < 6) {
$position = rand(0, $charactersNumber - 1);
$character = $characters[$position];
$code = $code . $character;
}
if (VoucherCode::where('code', $code)->exists()) {
dd('lol');
$this->generateUniqueCode();
}
return $code;
}
输入
<x-inputs.input size="6" type="number" name="voucher_amount" required>How many Voucher codes do you want
to
generate?
</x-inputs.input>
问题是它创建了模型,但代码 returns 每个模型的代码相同。
如何在一次调用中创建 15 个具有唯一代码字段的模型?
下面的 for 循环为我解决了这个问题。它最初是由某人发布的,但最近被删除了,所以我无法回答下面的问题,但感谢提供它的人!
$newVouchers = [];
for ($i = 0; $i < $request->voucher_amount; $i++) {
$newVouchers[] = VoucherCode::create([
'code' => $this->generateUniqueCode(),
'group_id' => $voucherGroup->id,
'used' => 0
]);
}
您可以稍后试用
do {
//generate unique code
$uniqueCode = strtoupper(substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 15));
} while (VoucherCode::where('code', $uniqueCode)->exists());
VoucherCode::create([
'code' => $uniqueCode,
'group_id' => $voucherGroup->id,
'used' => 0
]);
session()->flash('success', 'successfully created codes.');
return back();
您可以使用 Insert Statements 批量创建模型
$代金券=[]; 对于 ($i = 0; $i < $numberOfVoucher; $i++) { $代金券[] = [ 'code' => $this->generateUniqueCode(), 'group_id' => $voucherGroup->id, 'used' => 0 ]; } DB::table('users')->插入($vouchers);