【发布时间】:2018-11-18 18:12:10
【问题描述】:
我有两个模型:User 和 Book。我想用特定的二维码为每个用户创建一本书
$users = User::get();
$date = ... ;
$finalCount = 0;
$code = 150;
$userCount = count($users);
foreach ($users as $user) {
$book = new Book();
$book->unique_id = uniqid('', true);
$book->user_id = $user->unique_id;
$book->code = "PP-" . strval(mt_rand(100, 999)) . strval($code);
$book->create_date = $date;
$book->status = 'active';
$book->save();
$QRCode = new BaconQrCodeGenerator;
$file = public_path('/images/book/' . $book->code . '.png');
$QRCode->encoding('UTF-8')
->format('png')
->merge('/public/image/logo.png', .15)
->size(1000)
->generate($book->unique_id, $file);
if (File::exists($file))
$finalCount++;
$code++;
if ($finalCount == $userCount)
break;
}
调用此函数后,我为每个用户提供了 20 本书。我使用了if 语句来中断循环(if ($finalCount == $userCount)),但它不起作用。
我不明白这里发生了什么,我也没有任何错误日志
【问题讨论】: