Php 如何在Laravel Schema builder中传递变量
我无法使以下Laravel代码正常工作。我正在尝试以下代码。以下是表列数组: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)
$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
}
这是文件:
谢谢,就是这个。我不知道。再次感谢您的全面解释。