Php Laravel在同一模型中调用函数
我有一个有两个功能的模型。假设模型的名称是Php Laravel在同一模型中调用函数,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有一个有两个功能的模型。假设模型的名称是Cars。我试图调用一个返回数组的函数brand,以便在另一个getBrand函数中使用它 public static function getBrand($data) { $brandVariable = $this->brand(); for ($i=1; $i < count($brandVariable ) ; $i++) { //do something } } public stati
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;
}
公共静态函数getBrand($data){
$brandVariable=$this->brand();
对于($i=1;$i品牌A,
2=>品牌B,
);
返回数组值;
}
由于值位于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
}
}
公共静态函数getBrand($data=null){
$Cars=新车();
$brandVariable=$Cars::brand();
对于($i=1;$i
现场演示:
或
公共静态函数getBrand($data=null){
$brandVariable=汽车::品牌();
对于($i=1;$i
现场演示:您正在静态函数中调用
品牌
,而$此
在声明为静态
的方法中不可用
由于brand
声明为静态函数,因此可以使用以下方法之一调用该函数
如果在课堂上
self::brand();
ClassName::brand();
或
从课外
self::brand();
ClassName::brand();
这些函数是静态的,因此您应该使用
self::brand()
而不是
$this->brand()