Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel Schema builder中传递变量_Php_Variables_Laravel 4 - Fatal编程技术网

Php 如何在Laravel Schema builder中传递变量

Php 如何在Laravel Schema builder中传递变量,php,variables,laravel-4,Php,Variables,Laravel 4,我无法使以下Laravel代码正常工作。我正在尝试以下代码。以下是表列数组: $cols = array( 'inc' => array('id'), 'str' => array( 'name', 'email', 'password' ), ... ); 以下是函数: private function addCols($tableName, $cols)

我无法使以下Laravel代码正常工作。我正在尝试以下代码。以下是表列数组:

$cols = array(
   'inc' => array('id'),
   'str' => array(
              'name',
              'email',
              'password'
            ),
   ...
);
以下是函数:

private function addCols($tableName, $cols){
  foreach ($cols as $k => $type) {
   foreach ($cols[$k] as $col) {
    if(!Schema::hasColumn($tableName, $col)){
        Schema::table($tableName, function($table)
        {
          // Problem here $k and $col are `Undefined`

        }
        // Outside here $k and $col have values like `str`, `name`
    }
   }
  }
}

我对PHP不是很在行。

你要找的是
使用
关键字。它有助于匿名函数“继承”可能超出其作用域的现有变量:

    Schema::table($tableName, function($table) use ($k, $col)
    {
      // $k and $col are now defined
    }
这是文件:

谢谢,就是这个。我不知道。再次感谢您的全面解释。