doupai5450 2017-09-07 05:33
浏览 195
已采纳

Php Laravel在同一模型中调用一个函数

I have a model with 2 functions. Let's say the model's name is Cars. I am trying to call one function brand that returns an array so I can use it inside the other getBrand function.

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;
}

Since the values are in brand function, I need to pass it inside getBrand.

I am getting an error in the for loop. I tried in another file (local PHP not Laravel) and it is working fine. But in Laravel, it is not getting the expected result.

  • 写回答

3条回答 默认 最新

  • dop20345 2017-09-07 05:36
    关注

    Use Cars::brand because you declared functions as static

    public static function getBrand($data=null) {
        $Cars = new Cars();
        $brandVariable = $Cars::brand(); 
        for ($i=1; $i < count($brandVariable ) ; $i++) { 
            //do something
        }
    }
    

    Live demo : https://eval.in/856708

    Or

    public static function getBrand($data=null) {
        $brandVariable = Cars::brand(); 
        for ($i=1; $i < count($brandVariable ) ; $i++) { 
            //do something
        }
    }
    

    Live demo : https://eval.in/856712

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据