我有一个具有 2 个功能的模型。假设模型的名称是 Cars
。我正在尝试调用一个返回数组的函数 brand
,这样我就可以在另一个 getBrand
函数中使用它。
public static function getBrand($data) {
$brandVariable = $this->brand();
for ($i=1; $i < count($brandVariable ) ; $i++) {
//do something
}
}
public static function brand() {
$arrayValues = array(
1 => 'Brand A',
2 => 'Brand B',
);
return arrayValues;
}
由于值在 brand
函数中,我需要在 getBrand
中传递它。
我在 for
循环中遇到错误。我尝试了另一个文件(本地 PHP 而不是 Laravel)并且它工作正常。但是在 Laravel 中,它并没有得到预期的结果。
最佳答案
使用 Cars::brand
因为您将函数声明为静态
public static function getBrand($data=null) {
$Cars = new Cars();
$brandVariable = $Cars::brand();
for ($i=1; $i < count($brandVariable ) ; $i++) {
//do something
}
}
或者
public static function getBrand($data=null) {
$brandVariable = Cars::brand();
for ($i=1; $i < count($brandVariable ) ; $i++) {
//do something
}
}
关于Php Laravel 在同一个模型中调用一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46088519/