使用代码::
<?php
namespace App\Http\Controllers;
use Exception;
use Illuminate\Support\Str;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
public function index()
{
$count=15;
try {
DB::statement('truncate users');
DB::beginTransaction();
while ($count--){
$id = DB::table('users')->insertGetId( [
'name'=>'Sample'.$count,
'password'=>random_int(1000000,99999999)
]);
foreach (range(1,rand(1,3)) as $index ){
DB::insert('INSERT INTO posts (userid,title,body) VALUES (:userid,:title,:body)',[
'userid'=>$id,
'title'=>str::random(15),
'body'=>str::random(50),
]);
}
DB::commit();
}
}catch (\Exception $errors){
DB::rollBack();
Log::error($errors);
return "mission filed";
}
}