You could use a second function to get this to work
class LoopController extends Controller
{
public function create($i)
{
if (Auth::guard('admin')->user()->level == 2) {
Schema::connection('mysql')->create('tb' . $i, function ($table) {
$table->increments('id');
});
}
}
public function createMany()
{
for($i = 0; $i < 5; $i++) {
$this->create($i);
}
}
}
The create function takes in a parameter of $i, but is not called directly from the route, instead you'd call createMany() which has a for loop in it. This will call create 5 times, each time passing its iterater value in e.g. 0, 1, 2, 3, 4.
Alternatively, if you're wanting these functions to be invoked independently, you could use PHP's magic method to dynamically call the create function. For example you could grab all of the numbers after create and then call create(numbers);
class LoopController extends Controller
{
public function create($i)
{
if (Auth::guard('admin')->user()->level == 2) {
Schema::connection('mysql')->create('tb' . $i, function ($table) {
$table->increments('id');
});
}
}
public function __call($method, $parameters)
{
if (preg_match('/create([\d]+)/', $method, $matches)) {
return $this->create($matches[1]);
}
throw new \BadMethodCallException("Method " . get_class($this) . "::$method does not exist");
}
}
So now if you call LoopController->create48(); It will then call ->create(48);